Forums

Conditional sidebars according to category and children (3 posts)

  1. sflagg
    Member
    Posted 8 months ago #

    Hello

    For single post I want to create a conditional to bring in certain sidebars according to the categroy that the post is in.

    For example I would like to call in the sidebar_music.php when the post is in either music or a subcategory (child) of music. Music is a section or category of my site and is assigned cat ID 5

    Thus the music sidebar would appear in both these cases
    http://mysite.com/music/article
    http://mysite.com/music/concerts/article

    I assume the code would look something like this. I am a novice with PHP so I don't know the correct syntax but the perintent functions and logic are basically here. My main problem is I do not know how to define the category and ALSO the children of that category within this conditional.

    <?php

    if(is_category('5&&child_of=5'))

    include ('music_sidebar.php');

    elseif (is_category(9&&child_of=9))

    include ('tv_sidebar.php');

    elseif (is_category(8&&child_of=8))

    include ('dining_sidebar.php');

    ?>

  2. iridiax
    Member
    Posted 8 months ago #

  3. t31os
    Member
    Posted 8 months ago #

    See the examples here..

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

    Examples show how to check if is descendant(child)..

    TIP: regarding your code... it wasn't far off...

    if(something) {
    do this
    } else {
    do that
    }

    or

    if(something) :
    do this
    else :
    do that
    endif;

    You need to enclose your if/else statements....

    I personally found the curly brackets { and } easier when i started on PHP...

    Both are valid, no reason to use one over the other, just use which you prefer...

Reply

You must log in to post.

About this Topic