I have a magazine style website that has custom sidebars. If someone posts something under the category of Culture, I have culture sidebar that shows up. Under entertainment, the entertainment sidebar shows, etc. The problem I'm having is that the correct sidebar doesn't show if someone writes an article using a sub-category and not picking the parent category. So if someone just picks a subcategory of culture, the culture category won't show up. I've included my code from my sidebar.php page. Can anyone see what I'm doing wrong? Thanks!

 <aside id="secondary" class="widget-area">
 if ( in_category('culture') ) {
 dynamic_sidebar( 'sidebar-1' );
 }elseif ( in_category('entertainment') ) {
 dynamic_sidebar( 'sidebar-2' );

 }elseif ( in_category( 'arts'  ) ) {
 dynamic_sidebar( 'sidebar-3' );

}elseif ( in_category( 'eat'  ) ) {
    dynamic_sidebar( 'sidebar-4' );

}elseif ( in_category( 'opinion'  ) ) {
    dynamic_sidebar( 'sidebar-5' );

}elseif ( in_category( 'music'  ) ) {
    dynamic_sidebar( 'sidebar-6' );
 dynamic_sidebar( 'sidebar-1' );

  • Your code is good, but it's just that in_category() only returns true if the post is directly attached to the specified category/categories. So if you had 2 categories - one parent named Foo and the other a child named Bar, and only the Bar category that was selected for the post, then in_category( 'Bar' ) would return true, but not in_category( 'Foo' ).
    – Sally CJ
    Jul 28 at 3:49
  • Therefore, for in_category() to return true, you would need to select both the Foo and Bar categories (when you create/edit your post), or pass them both to in_category(), e.g. in_category( array( 'Foo', 'Bar' ) ).
    – Sally CJ
    Jul 28 at 4:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.