Skip to content

Instantly share code, notes, and snippets.

@jdhobbsuk
Created May 6, 2014 09:43
Show Gist options
  • Save jdhobbsuk/4bded33f4803c3be4363 to your computer and use it in GitHub Desktop.
Save jdhobbsuk/4bded33f4803c3be4363 to your computer and use it in GitHub Desktop.
is_child_term()
// is_child_term()
// -------------------------------------------------------------
function is_child_term($taxonomy, $parent_id){
$child_terms = get_term_children( $parent_id, $taxonomy );
$response = false;
foreach ( $child_terms as $term ):
if(is_tax($taxonomy, $term)):
$response = true;
endif;
endforeach;
return $response;
}
@jdhobbsuk
Copy link
Author

This snippet adds to the plethora of Conditional Tags that WordPress already has, but for some reason doesn't have this one.

Put simply, this function works much like is_tax(), but checks whether the current taxonomy term is a child of a specific taxonomy term. It's to be used within taxonomy-[name].php file, so you can tweak the layout within without creating separate theme files (taxonomy-[name]_[term].php). You'll most likely use it alongside is_tax().

I needed this because I needed to supply a completely different looking loop for a Videos section, and any of its sub terms.

if(is_tax( 'type', 10) || is_child_term('type', 10)):
    // loop for 'Videos' and sub terms
else:
    // loop for every other taxonomy term
endif;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment