2

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';
}
  • what is left is to check if those categories have posts right? – David Lee Feb 18 '17 at 2:12
  • @DavidLee Yes! Although I don't know if it's possible with this piece of code. – Tiago Feb 20 '17 at 9:59
0

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
) );
| improve this answer | |
  • 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 '17 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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