Assigning a Sidebar to a Category
-
I created a sidebar for a set of my Pages … It works.
I want to use this same sidebar for all posts in a related Category.
I’ve been looking through the forum & codex for a straightforward answer.
My category.php has mainly lots of functions of variety get_category.
I can’t figure how/where to put something like
<?php include(TEMPLATEPATH.”/sidebar_<slugname>.php”);?>New to WordPress – any help would be appreciated.
-
Something like:
<?php if( in_category( 'name' ) ) include(TEMPLATEPATH.'/sidebar_<slugname>.php');?>
should work.
Does this go in the pages.php or index.php under wp-content/themes near the <?php get_sidebar(); ?> ?
Or should I use the get_sidebar() function instead of the include() ?Using get_sidebar() would be better but you’d need your custom sidebar file to be called something like sidebar-<slugname>.php. Then you could use
<?php get_sidebar('<slugname>');?>
in place of<?php get_sidebar(); ?>
.<?php if( in_category( 'name' ) ) get_sidebar('<slugname>'); else get_sidebar();?>
thanks.
I put this code in index.php and page.php and the sidebar is now correct for the posts in the category. However, when I display the Category itself, (hover text says ‘View all posts filed under <catname>’) the sidebar is the same as that of the home page.
That sounds like an issue in your sidebar coding.
maybe, but i think it has to do with the fact that in_category() works for testing the category of posts but not testing what the current category archive page is. I tried using is_category() but that didn’t seem to work either.
I think I found the answer. I needed to copy archive.php to category-<catid>.php and then use the appropriate call to get_sidebar(). seems to work
- The topic ‘Assigning a Sidebar to a Category’ is closed to new replies.