I'm trying to add an if statement on the sidebars that are displays in POSTS. I want posts to show a specific sidebar if it's in category x and display a different sidebar if it's not in category x.

To do this, i'm trying to use the following code:

<?php if ( in_category( 'featured-listing' ) || ( in_category( 'listing-post' ) )) { ?>
    <?php get_sidebar(); ?>
<?php } else { ?>
   <?php get_sidebar2(); ?>
<?php } ?>

I've tried editing both the single.php and page.php to no avail. I thought maybe that since i removed the get_sidebar from my child theme that Wordpress was falling back to the Twentytwelve sidebar, but after trying a few things out it seems this isn't the case.

So - the summarize; i've tried editing both single.php and page.php to try and display different sidebars based on the category that the post is assigned to.

Thanks in advance.

2 Answers 2


get_sidebar() accepts a parameter $name. Then will look for a file sidebar-{$name}.php.

So what you can do is:

get_sidebar( get_post_type() ); // search for sidebar-post.php or sidebar-page.php


if ( in_category( array( 'featured-listing', 'listing-post' ) ) )
    get_sidebar( 'listing' ); // sidebar-listing.php
  • Thanks. The issue, though, is that i cannot find which file is placing the sidebar in individual posts. If i can find that, i can do what needs to be done; but i cannot, for the life of me, find what file is placing the sidebar as per original post. Commented May 23, 2013 at 16:33
  • @NikkiMather Some themes are calling get_sidebar() in footer.php or header.php. Use an editor that can search in a whole directory.
    – fuxia
    Commented May 23, 2013 at 16:37
  • The Twentytwelve theme (i'm using a child theme of that) doesn't have the sidebar in the footer.php or header.php files. This is really confusing. Commented May 23, 2013 at 16:54
  • In Twenty Twelve the function is called in index.php at the bottom.
    – fuxia
    Commented May 23, 2013 at 16:56
  • I tried removing it from index.php too, however the sidebar is still there. Commented May 23, 2013 at 17:55

As you want to define separate sidebar for :


2.and page

3.in post for category

Place below code in your sidebar.php

if( 'post' == get_post_type()){
    $categories = get_the_category();
    foreach( $categories as $category ){
    //search for template for category
    $cat_template = pathinfo( locate_template( $sidebar_template ) );
    if( !empty( $cat_template )){
        $cat_template_name=explode( '-', $cat_template['filename'] , 2);
        get_sidebar( $cat_template_name[1] );
        get_sidebar( 'post' );


and create

1.sidebar-page.php for page

2.sidebar-post.php for post

3.sidebar-category_slug.php for post category

what code does:

1.If get_post_type is equal to post then , I retrieve all categories for current post and test is their any sidebar template for category it.if yes then sidebar for category will call ( sidebar-category_slug.php )else sidebar for post will call ( sidebar-post.php ).

2.If get_post_type is equal to page then sidebar for page will call ( sidebar-page.php ).

Note: code drived only for assuming current condition.We can modify it for different levels for example if we want to separate sidebar template for sub category.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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