WordPress.org

Ready to get started?Download WordPress

Forums

Sidebar Content That Changes Depending On The Category? (8 posts)

  1. grypesagon
    Member
    Posted 8 years ago #

    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.

  2. jackosh
    Member
    Posted 8 years ago #

    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')

  3. jackosh
    Member
    Posted 8 years ago #

    To clarify, your sidebar should look like this:

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

    CONTENT FOR WHEN PAGE IS INDEX.PHP

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

    CONTENT FOR IF IT IS SINGLE

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

    CONTENT FOR IF IT IS CATEGORY 5

    <?php } ?>

  4. grypesagon
    Member
    Posted 8 years ago #

    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?

  5. moshu
    Member
    Posted 8 years ago #

    Yes, you just have to read the Codex :)
    http://codex.wordpress.org/Conditional_Tags

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

  6. grypesagon
    Member
    Posted 8 years ago #

    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. :-)

  7. moshu
    Member
    Posted 8 years ago #

    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 :)

  8. grypesagon
    Member
    Posted 8 years ago #

    will do. thanks a bunch.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.