Conditional that checks if a current category has any subcategory with posts

I’m looking for a way to make a conditional rule that checks, in a category archive template, if the current category has any subcategories with posts.

More specifically, the posts are “products” in a Woocommerce setup.

So far I was only able to check if the current category has subcategories with the code below. But even after some searching I wasn’t able to advance any further.

$term = get_queried_object();

$children = get_terms( $term->taxonomy, array(
'parent'    => $term->term_id,
'hide_empty' => false
) );
if($children) {
    echo 'something';
}

1 Answer
1

Please set hide_empty to true which will fetch only categories which is assigned to post.

$term = get_queried_object();

$children = get_terms( 'category', array(
'parent' => $term->term_id,
'hide_empty' => true
) );

Leave a Comment