1
votes

My goal is to display conditional content in an archive page in Wordpress based on whether post belongs to the main category of custom taxonomy and any subcategories of the certain taxonomy category. I know how to achieve this for the plain post categories and child subcategories.

if (is_category('dogs') || cat_is_ancestor_of('dogs', get_query_var( 'cat' )) { echo 'Successs';

The first part works fine

is_tax('advert_category', 380) ) ||

but how do I check whether the post belongs to a subcategory of taxonomy category 380?

1

1 Answers

0
votes

I believe is_tax() is the wrong function to use to check whether a post has a certain term or not. is_tax() simply checks if you are on a taxonomy archive or not.

You should be using has_term(), which does just that - it checks if a post has a certain term.

When we start looking at terms, the get_term_children function becomes available to us:

<?php
$term_id = 380;
$taxonomy_name = 'advert_category';
$term_children = get_term_children( $term_id, $taxonomy_name );

echo '<ul>';
foreach ( $term_children as $child ) {
    $term = get_term_by( 'id', $child, $taxonomy_name );
    echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a></li>';
}
echo '</ul>';
?> 

I hope this helps!