Forums

post_is_in_descendant_category seems to not filter siblings (1 post)

  1. fgshepard
    Member
    Posted 1 year ago #

    when i use post_is_in_descendant_category it seems to not filter for siblings. here's my cat structure (by slug):

    rethinking_religion
    -blog
    --dialogue
    --columnist
    -media
    --episode
    --transcript

    i've been asked to use different header images for the blog and the media sections (they are siblings under "rethinking_religion"). i use this code in the header to filter:

    if (in_category('Blog') || post_is_in_descendant_category(get_term_by('slug','blog','category'))) {
    	$header_image = "header-RR-blog_03.jpg";
    	$navbar_image = "header-RR-blog_05.jpg";
    }
    elseif (in_category('Media') || post_is_in_descendant_category(get_term_by('slug','media','category')) || is_page('podcasts-rr')) {
    	$header_image = "header-RR-media_03.jpg";
    	$navbar_image = "header-RR-media_05.jpg";
    }

    unfortunately, the post_is_in_descendant function doesn't seem to be filtering siblings. so the media posts are returning true for the "blog" portion of the if statement even though they aren't children of that category. any ideas?

    here's the post_is_in_descendant_category code for easy reference:
    function post_is_in_descendant_category( $cats, $_post = null )
    {
    foreach ( (array) $cats as $cat ) {
    // get_term_children() accepts integer ID only
    $descendants = get_term_children( (int) $cat, 'category');
    if ( $descendants && in_category( $descendants, $_post ) )
    return true;
    }
    return false;
    }

Topic Closed

This topic has been closed to new replies.

About this Topic