Forums

Assigning a Sidebar to a Category (8 posts)

  1. myfairladyah
    Member
    Posted 2 years ago #

    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.

  2. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    Something like:

    <?php if( in_category( 'name' ) ) include(TEMPLATEPATH.'/sidebar_<slugname>.php');?>

    should work.

    http://codex.wordpress.org/Template_Tags/in_category

  3. myfairladyah
    Member
    Posted 2 years ago #

    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() ?

  4. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    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();?>
  5. myfairladyah
    Member
    Posted 2 years ago #

    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.

  6. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    That sounds like an issue in your sidebar coding.

  7. myfairladyah
    Member
    Posted 2 years ago #

    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.

  8. myfairladyah
    Member
    Posted 2 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic