On WooCommerce I am using the archive-product.php template page. I want to show a list of the sub-categorys from the displayed category.

So as the user changes category the list of subcaegories will change to match.

I can get the sub categories for a specific parent category, but I am stuck getting something like if($cat->category_parent == $this)

The code I am using to get subcategorys listed for parent Id=9

$args = array(
  'taxonomy'     => 'product_cat',
'orderby'      => 'name'
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 9) {
    $category_id = $cat->term_id;
    echo '<a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>'; 

But how do I make it dynamic so each category will show its own subcategories?

1 Answer 1


You can make use of get_term_children() instead of get_categories()

You can try something like this

$term = get_queried_object();
$term_id = $term->term_id;
$taxonomy_name = $term->taxonomy;

$termchildren = get_term_children( $term_id, $taxonomy_name );

echo '<ul>';
foreach ( $termchildren as $child ) {
    $term = get_term_by( 'id', $child, $taxonomy_name );
    echo '<li><a href="' . get_term_link( $term, $taxonomy_name ) . '">' . $term->name . '</a></li>';
echo '</ul>';
  • thank you @Pieter Goosen it worked like charm Sep 16, 2021 at 14:16

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.