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
) );
  • Oh I see, but I don't want to hide empty categories. I'm sorry if it wasn't explicit, but what I want to do is add some html to a category page if that same category page has at least one subcategory with posts.
    – Tiago
    Feb 17, 2017 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.