• In my category.php, I’ve been using a conditional statement to select specific sidebars for specific categories or groups of categories, like this:

    <?php if (is_category( array( 'cheddar', 'swiss'))) {
    get_sidebar('cheese');
    } else
    if (is_category( array( 'apples', 'cherries', ))) {
    get_sidebar('fruit');
    } else {
    get_sidebar();
    } ?>

    Is there a way to call a sidebar to be used with a category and all of that category’s children?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Try it with:

    $current_cat = get_query_var('cat');
    if(is_category(3) || cat_is_ancestor_of( 3, $current_cat )) {
    // get sidebar for category 3 and all of its children
    }

    Thread Starter eable

    (@eable)

    After I posted I found this description of how to use a post_is_in_descendant_category function.

    cat_is_ancestor_of looks useful. Thank you!

    I need to make some test cases to feel through how practical application of cat_is_ancestor_of is different from post_is_in_descendant_category. Interesting.

    you could always do something like this…..

    if ( is_category('catgory1') || is_category('category2) {
    get_sidebar('sidebar1');
    }
    
    elseif ( is_category('category3') {
    get_sidebar('sidebar2')
    }
    
    else {
    get_sidebar('sidebar3')
    }

    the || means OR in PHP.

    eable… I tried using your conditional statement but can’t seem to get it to work (I’m a newb to php).

    I replaced:
    <?php get_sidebar(); ?>
    with:
    <?php if (is_category( array( ‘jewel’, ‘lotus’))) {
    get_sidebar(‘JewelsLotus’);
    } else
    if (is_category( array( ‘almanac’ ))) {
    get_sidebar(‘almanac’);
    } else {
    get_sidebar();
    } ?>

    Is there something I’m doing wrong, or something more I need to do?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom sidebar for category children?’ is closed to new replies.