Yes, there are two functions you can use:
For the default category
taxonomy, you can use cat_is_ancestor_of()
:
// Retrieve category object by slug:
$parent_cat = get_category_by_slug( 'clothes' ); // term object
$child_cat = get_category_by_slug( 't-shirts' ); // term object
/* Or retrieve category ID by name:
$parent_cat = get_cat_ID( 'Clothes' ); // term ID
$child_cat = get_cat_ID( 'T-Shirts' ); // term ID
*/
if ( cat_is_ancestor_of( $parent_cat, $child_cat ) ) {
echo 't-shirts is child of clothes';
}
For custom taxonomies, you would use term_is_ancestor_of()
:
// Set the custom taxonomy's slug:
$taxonomy = 'my_taxonomy';
// Retrieve term object by slug:
$parent_cat = get_term_by( 'slug', 'clothes', $taxonomy );
$child_cat = get_term_by( 'slug', 't-shirts', $taxonomy );
/* Or name, if you want:
$parent_cat = get_term_by( 'name', 'Clothes', $taxonomy );
$child_cat = get_term_by( 'name', 'T-Shirts', $taxonomy );
*/
if ( term_is_ancestor_of( $parent_cat, $child_cat, $taxonomy ) ) {
echo 't-shirts is child of clothes';
}
And note that cat_is_ancestor_of()
is really a wrapper around (i.e. it uses) term_is_ancestor_of()
which accepts either term ID or object, so for examples, you could do cat_is_ancestor_of( 1, 2 )
and term_is_ancestor_of( 3, 4, 'my_taxonomy' )
whereby the numbers like 1
and 3
are term IDs.