Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I dont understand why my sidebar isn't changing over to the second one on the other pages. I've gone in an told the pages that if your not on this page show sidebar 2. So the home page and blog page have a sidebar 1 and the rest of the pages have sidebar 2. But it's not working.

My code on my main theme layout looks like this:

<?php if(is_page('blog')) { ?>
    <?php include(TEMPLATEPATH.'/sidebar2.php'); ?>
<?php } else { ?>
    <?php include(TEMPLATEPATH.'/sidebar.php'); ?>
<?php } ?>  

<?php get_footer(); ?>

So if I understand it correctly, if the page is blog show sidebar 1 if not show sidebar 2. I have sidebar.php and sidebar-2.php pages and they have widgets in them so I know when it's working.

Yet I don't see it doing anything. Is there something in the functions.php file I've forgotten to place in?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Your main problem is that the is_page() conditional will not return true if it is included inside or after the loop.

Your other issue which is not necessarily related to the issue at hand is the way you are including your sidebars. WordPress comes with a handy little function called get_sidebar(). If you have more than one sidebar you can pass a parameter to the function that lets it know what sidebar you want.

You need to name your sidebars sidebar-{sidebar_name}.php. So if you wanted to include sidebar1 you would name your file sidebar-sidebar1.php and to include it is simply get_sidebar( 'sidebar1' )

To solve your is_page() problem you can either call wp_reset_query() after your loop or assign the conditional return boolean to a variable before the loop then use it when including your sidebar.

//Somewhere before the loop
$page_var = is_page( 'blog' ) ? true : false;

//loop stuff

if ( $page_var ) {
     get_sidebar( 'sidebar1' );
} else { get_sidebar( 'sidebar2' ); }
share|improve this answer
1  
Mostly personal preference here, but I'd use get_sidebar() for the first sidebar and then get_sidebar( 'blog' ) for the second. This uses the files sidebar.php and sidebar-blog.php. Better yet, that means sidebar.php is the fallback if sidebar-blog.php ever doesn't exist. Also, it's more semantic. –  mrwweb May 27 '12 at 18:55
    
Thanks guys! Fixed it! –  kia4567 May 28 '12 at 6:36
    
Good point mrwweb. –  Chris_O May 28 '12 at 6:38

Do you have the blog page selected under the "Posts page" option of the "Front page displays" Reading Setting? If so then you need to update the conditional from is_page('blog') to is_home().

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.