When a user clicks on a product category from the WooCommerce shop page, instead of the default, I need it to look like the below design, the second picture marks what I mean:

Mock without mark up

Mock with mark up

So basically, I need to iterate through each subcategory, display it, check it it has subcategories, and, if it does, display them. I'm currently able to get the sub-subcategory level using the answer from this post and hooking it to "woocommerce_after_subcategory":


I just need help checking to see if those subcategories have a subcategory and then displaying them.

Please let me know if I need to further explain something! Thanks!

1 Answer 1


I solved this by modifying the function linked to in the question to make it recursive. The comments explain my changes:

function woocommerce_subcats_from_parentcat_by_ID( $parent_cat_ID ) {
$args = array(
   'hierarchical' => 1,
   'show_option_none' => '',
   'hide_empty' => 0,
   'parent' => $parent_cat_ID,
   'taxonomy' => 'product_cat'
$subcats = get_categories($args);

if ( $subcats ) { // added if statement to check if there are subcategories
  echo '<ul class="product-category-list">';
    foreach ($subcats as $sc) {
      $link = get_term_link( $sc->slug, $sc->taxonomy );
        echo '<li><a href="'. $link .'">'.$sc->name.'</a></li>';
        woocommerce_subcats_from_parentcat_by_ID( $sc->term_id ); // function calls itself
  echo '</ul>';
} else {
  return; //return if no subcategories
    }} // last bracket kept being pushed out of code block if I used a line break

I'm sure there is a more efficient way to do this instead of having the function call itself every time but this works.

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.