EDIT from COMMENTS
All I want to do, in this case, is display a header(header)2 -IF-a post has a certain category checked. I might have "Grizzly bears" only, or might have both "Grizzly bears" and its child "Grizzly bears fishing," or I might have another child "Grizzly bears eating" but as long as that "Grizzly bears" category is checked, I want the -THEN- statement to happen.
We can still use the same function that we have created in the ORIGINAL ANSWER, but we will aplly it a bit different. We will need to get the categories the post belongs to and then check if Grizzly bears are among them
$parent_cat = 1, //Pass the ID, this will ID for 'Grizzly bears'
$descendants = get_term_descendants( $parent_cat );
$post_terms = get_the_category( get_the_ID() );
$ids = wp_list_pluck( $post_terms, 'term_id' );
if ( $decendants && !in_array( $parent_cat, $ids ) ) {
if ( has_category( $decendants ) ) {
// Post belongs to parent cat descendants but not parent cat
}
} elseif ( in_array( $parent_cat, $ids ) ) {
// Post belongs to the parent category
} else {
// Post does not belong to parent cat or any of its decendants
}
ORIGINAL ANSWER
Your question is a bit hard to understand, but I will answer to how I read it.
If you need to apply a certain header to each to the top level term and all of its descendants, you can write your function using get_terms()
to return all descendants from the given top level parent
FEW NOTES BEFORE CODE:
The code is untested and might be buggy. Be sure to test this locally first with debug turned on
The code requires at least PHP 5.4
It can be quite verbose as we need to do a lot of work to get all the descendants from a top level term. Note, I have explained this, but for performance, make sure that you pass the ID rather than slug to the function, and do not use names
CODE
function get_term_descendants( $term = '', $taxonomy = 'category' )
{
// First make sure we have a term set, if not, return false
if ( !$term )
return false;
// Validate and sanitize taxonomy
if ( 'category' !== $taxonomy ) {
$taxonomy = filter_var( $taxonomy, FILTER_SANITIZE_STRING );
// Make sure the taxonomy is valid
if ( !taxonomy_exists( $taxonomy ) )
return false;
// Make sure our taxonomy is hierarchical
if ( !is_taxonomy_hierarchical( $taxonomy ) )
return false;
}
// OK, we have a term and the taxonomy is valid, now we need to validate and sanitize the term
if ( is_numeric( $term ) ) {
$term = filter_var( $term, FILTER_VAILDATE_INT );
} else {
$term_filtered = filter_var( $term, FILTER_SANITIZE_STRING );
$term_object = get_term_by( 'slug', $term_filtered, $taxonomy );
$term = filter_var( $term_object->term_id, FILTER_VAILDATE_INT );
}
// Everything is sanitized and validated, lets get the term descendants ids
$term_descendants = get_terms( $taxonomy, ['child_of' => $term, 'fields' => 'ids'] );
// Check if we have terms
if ( !$term_descendants )
return false;
// We have made it, return the descendants
return $descendants;
}
Our function get_term_descendants()
will now either hold an array of descendant term ID's of the term passed to the function or will return false if the passed term dont have descendants
We can now use our function and pass the returned array of ids directly to has_category()
NOTE: You can either pass the parent term ID or slug to the function. Do not use names as hierarchical terms can have duplicate names accross different hierarchies. Also note, passing the slug to the function is a bit more expensive because we will use get_term_by()
to get the term ID from the slug. For performance, try to always pass the ID
$parent_cat = 1, //Pass the ID, can pass the slug, but reread the note above
$descendants = get_term_descendants( $parent_cat );
if ( $decendants ) {
$term_array = array_merge( $parent_cat, $descendants );
} else {
$term_array = $parent_cat;
}
// Now we can check if the post belongs to the parent cat or one of his descendants
if ( has_category( $term_array, $post ) ) {
// Post belongs to parent cat or one of his descendants
} else {
// Post does not belong to the parent or any of his descendants
}