Lego2012
12/13/2016 - 3:21 PM

if conditional for targeting all child categories of a category

if conditional for targeting all child categories of a category

<?php
//* Do NOT include the opening php tag shown above. Copy the code shown below.

function is_category_tree( $branch='' ) {
    $cat = get_term_by( 'name', $branch, 'category' );
    if( !$cat ) $cat = get_term_by( 'slug', $branch, 'category' );
    if( !$cat ) $cat = get_term_by( 'id', (int)$branch, 'category' );
    $this_cat = get_query_var( 'cat' );
    if( $cat && is_category() && ( $cat->term_id  == $this_cat || in_array( $cat->term_id, get_ancestors( $this_cat, 'category' ) ) ) )
      return true;
    else
      return false;
}

<!-- Usage: -->

if ( is_category_tree( 'recipes' ) ) {
}