Support » Themes and Templates » Sidebar Content That Changes Depending On The Category?

  • Is it possible?

    I’m not super well versed in PHP but it looks like it may be somehow plausible to make a definition within sidebar.php that tells it:
    “If this single post using template single.php is under catergoy 4 use image 4.jpg in the sidebar.
    If this is using category-5.php use image 5.jpg in the sidebar.
    If this is using index.php use image 0.jpg in the sidebar.”

    is there a coding function that can do something like this?

    the only thing I can think of is not using the sidebar.php file at all and just manually editing the code in each respective template (which would make all single posts use the same image instead of the image of thier category which would be sad but doeable) and that would just take a really long time.

Viewing 7 replies - 1 through 7 (of 7 total)
  • yes this is possible…there should be code at the top of sidebar.php that says something like this:

    <?php if( is_single() ) { ?>

    Before the end of this if statement, add a line like this:

    <?php } elseif( is_category() ) { ?>

    and put the content after this line. If you want it to occur on a specific category change the is_category() to is_category(‘cat_name’)

    To clarify, your sidebar should look like this:

    <?php if( is_home() ) { ?>


    <?php } elseif( is_single() ) { ?>


    <?php } elseif( is_category(‘5’) ) { ?>


    <?php } ?>

    Thank you so much. I saw the line in the existing template that said if(is_home) and figured this could be applied in other ways. That’s exactly what I was looking for as far as categories.

    However is there a way to specify pages the same way as categories? Such as, for page 2 could I use: <?php } elseif(is_page(‘2’)){?>?

    Also can I further specify the “single” so it identifies what category the single post came from? Like… using the “child_of” phrase or something so I can have a different image for a single post that is categorized under category 2 than the image that shows up for a single post that is categorized under category 4?

    Yes, you just have to read the Codex 🙂

    Edit. Although I don’t know of any way to do what you ask in the last paragraph…

    I shoulda known it was in there somewhere. I did read through a lot I just never found that section. (I really have to brush up on my knowledge of the terminology, it seems evertime I’m looking for something I can’t find it’s cause I’m calling it the wrong name in my head.)

    Thanks. 🙂

    Yes, it takes some times until you learn the “lingo”.

    On a second reading of your last paragraph… See the Plugins section in the codex and serach for Category Image, Category Icon or something like that 🙂

    will do. thanks a bunch.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Sidebar Content That Changes Depending On The Category?’ is closed to new replies.