Get the sub category of the current product category page
Get the sub category of the current product category page. This code is applied if you are viewing the product archive page. The codes below and a sample category hierarchy list.
Example:
- Category Parent
- Category 1
- Category 2
- Category 3
- Category 4
- Category 1.1
- Category 2.1
- Category 3.1
- Category 4.1
Here’s a code:
$category_object = get_queried_object(); if($_GET['debug']){echo "\n ::: $category_object->parent ::: \n";} if( $category_object->parent == 0 ){ $parent_id = $category_object->term_id; }else{ $parents_1 = get_term_by( 'id', $category_object->parent, 'product_cat' ); if( $parents_1->parent == 0 ){ $parent_id = $parents_1->term_id; }else{ $parents_2 = get_term_by( 'id', $parents_1->parent, 'product_cat' ); $parent_id = $parents_2->term_id; } } $terms = get_terms( 'product_cat', array( 'parent' => $parent_id,'hide_empty' => true ) ); if( $terms && $parent_id ){ foreach ( $terms as $term ) { ?> <li class="slug . ' ' . $class; ?>"> <a href="term_id ); ?>">name; ?></a> <small class="count">count; ?></small> <div class="clear"></div> </li> <li class="no-item">No Sub Categories</li><?php }