Conditional tag affecting taxonomy term and its children?

I am wondering how can I write a conditional tag that affects a custom taxonomy slug and its children?

Found this in WordPress Codex:

is_tax( 'flavor', 'mild') 

When the archive page for the flavor
taxonomy with the slug of ‘mild’ is
being displayed.

However with this code only ‘mild’ slug archive page is affected and not it’s children taxonomy terms.

Thanks!

1 Answer
1

I have a handy little function that i based on post_is_in_descendant_category function
it expends the is_tax to check if its the term or any of his children

function is_or_descendant_tax( $terms,$taxonomy){
    if (is_tax($taxonomy, $terms)){
            return true;
    }
    foreach ( (array) $terms as $term ) {
        // get_term_children() accepts integer ID only
        $descendants = get_term_children( (int) $term, $taxonomy);
        if ( $descendants && is_tax($taxonomy, $descendants) )
            return true;
    }
    return false;
}

and you use it just the same so instead of:

is_tax('flavor', 'mild');

you put:

is_or_descendant_tax(get_term_by( 'name', 'mild', 'flavor' ), 'flavor');

or if you are using term id instead of name then:

is_or_descendant_tax(12, 'flavor');

and you are set.

Leave a Comment