conditional tags in wordpress sidebar

Discussion in 'WordPress' started by sontion, Nov 8, 2008.

  1. #1
    Hey ,

    I need a certain set of widgets for a single page , and for the rest of the pages / posts i need the standard sidebar widgets .

    i found out that it can be done with conditional tags in wordpress . but i am not getting it to work properly . i have read few pages / posts on the the topic , but still m getting it wrong .

    The page for which i want a different sidebar has ID : 2

    This is my sidebar.php


    <?php global $wp_theme_options; ?>
    <div class="w300- sidebar">

    <!--sidebar.php-->

    <?php if ( !function_exists('dynamic_sidebar')
    || !dynamic_sidebar('Right Sidebar') ) : ?>

    <?php endif; ?>

    </div>

    can anyone tell me how to put it .

    thanks a lot
     
    sontion, Nov 8, 2008 IP
  2. magicinthedesert

    magicinthedesert Peon

    Messages:
    143
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try using this:
    
    <?php if (is_page('2')) { ?>
    sidebar 2
    <?php } else { ?>
    other sidebars
    <?php } ?>
    
    Code (markup):
    Replace sidebar 2 with what you want in your sidebar on page 2. Replace other sidebars with what you want in all your other sidebars.

    If you want both sidebars to contain widgets, you'll need to see how many dynamic sidebars your theme has registered. If you have 2, then you can use each on for the different code. If you only have one, you'll need to register a second dynamic sidebar to your theme. I talk about how to do this for creating a widgeted footer, but the theory still applies to sidebars. You can find that article here: How to Widgetize Your WordPress Footer.

    For example:
    
    
    <?php global $wp_theme_options; ?>
    <div class="w300- sidebar">
    
    <!--sidebar.php-->
    
    <?php if (is_page('2')) { ?>
    <?php if ( !function_exists('dynamic_sidebar')
    || !dynamic_sidebar('Sidebar2') ) : ?>
    
    <?php endif; ?>
    
    <?php } else { ?>
    <?php if ( !function_exists('dynamic_sidebar')
    || !dynamic_sidebar('Right Sidebar') ) : ?>
    
    <?php endif; ?>
    
    <?php } ?>
    
    </div>
    
    Code (markup):
     
    magicinthedesert, Nov 8, 2008 IP
  3. sontion

    sontion Peon

    Messages:
    282
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks a lot for the detailed reply . i will try it and get back to you . thanks again .
     
    sontion, Nov 9, 2008 IP
  4. sontion

    sontion Peon

    Messages:
    282
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hey ,

    i created sidebar 'Blog Sidebar' . added 3 widgets to it .

    But now when i put in this , it still doesnt work . can you have a look and tell me where i have gone wrong . really appreciate all the help .

    Heres the sidebar :

    <?php global $wp_theme_options; ?>
    <div class="w300- sidebar">

    <!--sidebar.php-->

    <?php if (is_page('2')) { ?>
    <?php if ( !function_exists('dynamic_sidebar')
    || !dynamic_sidebar('Blog Sidebar') ) : ?>

    <?php endif; ?>

    <?php } else { ?>
    <?php if ( function_exists('dynamic_sidebar')
    || !dynamic_sidebar('Right Sidebar') ) : ?>

    <?php endif; ?>

    <?php } ?>

    </div>
     
    sontion, Nov 9, 2008 IP
  5. magicinthedesert

    magicinthedesert Peon

    Messages:
    143
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Is there an error that comes up on your page?

    The way I would trouble shoot this is take out the code and try just calling to the new widgeted sidebar you created. Use the code below and see if it works. If it doesn't then you know you're problem is with the new widgeted sidebar. Let me know what happens.

    
    <?php global $wp_theme_options; ?>
    <div class="w300- sidebar">
    
    <!--sidebar.php-->
    
    <?php if ( !function_exists('dynamic_sidebar')
    || !dynamic_sidebar('Blog Sidebar') ) : ?>
    
    <?php endif; ?>
    
    </div>
    Code (markup):
     
    magicinthedesert, Nov 9, 2008 IP
    sontion likes this.
  6. sontion

    sontion Peon

    Messages:
    282
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hey ,

    the above code works perfect . the widgets in the 'Blog Sidebar' comes up in all the pages .

    also when i was having problem while having both the dynamic sidebars . there wasnt any error . no widgets would come up then . the sidebar area would remain blank .

    So its not the Blog Sidebar which is having problem (hopefully) .

    waiting for your reply .

    If you want access to my blog - i would be more than willing to share it with you .
     
    sontion, Nov 9, 2008 IP
  7. magicinthedesert

    magicinthedesert Peon

    Messages:
    143
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I sent you a PM
     
    magicinthedesert, Nov 11, 2008 IP
  8. magicinthedesert

    magicinthedesert Peon

    Messages:
    143
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Looks like I was wrong about using is_page in the sidebar. WordPress wasn't recognizing it. The solution implemented was to create a page template that called a second sidebar file with the dynamic sidebar for the blog page.

    1. Create a file called sidebar2.php and upload it to your theme files. The code in this file is:
    
    <?php global $wp_theme_options; ?>
    <div class="w300- sidebar">
    
    <!--sidebar.php-->
    
    <?php if ( !function_exists('dynamic_sidebar')
    || !dynamic_sidebar('Blog Sidebar') ) : ?>
    
    <?php endif; ?>
    
    </div>
    
    Code (markup):
    2.Create a page template and upload it to your theme files. Replace
    <?php get_sidebar(); ?>
    Code (markup):
    with
    <?php include(TEMPLATEPATH. "/sidebar2.php"); ?>
    Code (markup):
    3. Login to WordPress and go to manage page for the page you want to change the sidebar on. Change the page template to your new template and click save
     
    magicinthedesert, Nov 13, 2008 IP
  9. sontion

    sontion Peon

    Messages:
    282
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #9
    the problems solved now . thanks a lot .. : )
     
    sontion, Nov 13, 2008 IP