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.
get_sidebar()
accepts a parameter$name
. Then will look for a filesidebar-{$name}.php
.So what you can do is:
or:
As you want to define separate sidebar for :
1.post
2.and page
3.in post for category
Place below code in your
sidebar.php
and create
1.
sidebar-page.php
for page2.
sidebar-post.php
for post3.
sidebar-category_slug.php
for post categorywhat 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.