Conditional tag taxonomy term and its children

Background:

WordPress has a great set of conditional tags which make developers and theme designers life much easier. these functions are good at what they were meant to do but some times they are just not enough, so this is a first of a set of custom conditional tags functions that I've written and use many time before, and maybe you'll find them handy.

Problem:

When using is_tag() or is_tax() or is_category() it is limited to search if the current page queried is a tag,taxonomy term or category archive. You can also specify the specific term you are looking for by passing it's ID,Slug or name for example Say my taxonomy is named 'sites' and i want to check if the current page is a term named 'fun' in that taxonomy then i can use:

if (is_tax( 'sites', 'fun')){
//do my stuff on the fun page my_function();
}

This all works just fine but what about if i want to "do my stuff" on the fun page or any of its children for example say i have an hierarchy of terms set up like so:

  • Fun
    • Games
    • Media
    • Social
  • Not Fun
    • Games
    • Media
    • Social

So I want my function to run on the fun page or any of its children using is_tax would be:

if (is_tax( 'sites', 'fun')||is_tax( 'sites', 'games')||is_tax( 'sites', 'media')||is_tax( 'sites', 'social')){
//do my stuff on the fun page my_function();
}

Again not so bad but what if Fun had 100's of terms??? and since both "Fun" and "Not Fun" have the same terms i have to make sure that i use either the correct term id or slug, well its starting to get a little messy.

Solution:

I wrote this 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

/* Conditional Tag to check if its a term or any of its children
*
* @param $terms - (string/array) list of term ids
*
* @param $taxonomy - (string) the taxonomy name of which the holds the terms. 
*/
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;
}

Usage:

If i know the parent term or the term i want to check ID then it simple, for example say the term id of "Fun" is 12 then:

if (is_or_descendant_tax(12, 'sites')){
//do my stuff on the fun page or any of its children my_function();
}

But that's only when i know the ID, what if i don't? Still simple:

if (is_or_descendant_tax(get_term_by( 'name', 'fun', 'sites' ), 'sites')){
//do my stuff on the fun page or any of its children my_function();
}

Install:

Copy this code in to your theme's functions.php or create a plugin file for it.

Parameters:

$terms - (string/array) list of term ids. required

$taxonomy - (string) the taxonomy name of which the holds the terms. required

I'll be adding a few more custom Conditional tags i have in the next few weeks, So until then , as always feedback is more the welcome.

Ohad Raz

WordPress Consultant, a WordPress Developer and a WordPress Freelancer With over 10 years experience in architecting web sites and applications. WordPress Development moderator and somethimes Plugin Developer.