WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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
share|improve this answer
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. – Nikki Mather May 23 '13 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. – toscho May 23 '13 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. – Nikki Mather May 23 '13 at 16:54
In Twenty Twelve the function is called in index.php at the bottom. – toscho May 23 '13 at 16:56
I tried removing it from index.php too, however the sidebar is still there. – Nikki Mather May 23 '13 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.

share|improve this answer

Your Answer


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.