WordPress.org

Ready to get started?Download WordPress

Forums

Custom sidebar for category children? (5 posts)

  1. eable
    Member
    Posted 2 years ago #

    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?

  2. keesiemeijer
    moderator
    Posted 2 years ago #

    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
    }
  3. eable
    Member
    Posted 2 years ago #

    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.

  4. wspencer
    Member
    Posted 2 years ago #

    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.

  5. Jon Fergus
    Member
    Posted 1 year ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic