I want to get all woocommerce categories with subcategory but max to third degree, example:
- Main Category
-
- First subcategory of Main
-
- Second subcategory of Main
-
-
- First subcategory of Second
-
-
- Third subcategory of Main
-
- Fourth subcategory of Main
-
-
- First subcategory of Fourth
-
-
-
-
- First subcategory of Fourth's First ------ THIS ONE SHOULD NOT BE DISPLAY!!
-
-
-
-
-
- Second subcategory of Fourth's First ------ THIS ONE SHOULD NOT BE DISPLAY!!
-
-
$args = array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'parent' => 0
);
$product_cat = get_terms( $args );
foreach ($product_cat as $parent_product_cat) {
echo '<ul class="nav-menu__cats"><li><a href="'.get_term_link($parent_product_cat->term_id).'">' .$parent_product_cat->name. '</a><ul>';
$child_args = array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'parent' => $parent_product_cat->term_id
);
$child_product_cats = get_terms( $child_args );
foreach ($child_product_cats as $child_product_cat) {
echo '<li><a href="'.get_term_link( $child_product_cat->term_id ).'">' .$child_product_cat->name. '</a></li><ul>';
$child2_args = array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'parent' => $child_product_cat->term_id
);
$child2_product_cats = get_terms( $child2_args );
foreach ($child2_product_cats as $child2_product_cat) {
echo '<li><a href="'.get_term_link( $child2_product_cat->term_id ).'">' .$child2_product_cat->name. '</a></li></ul>';
}
}
echo '</ul></li></ul>';
}
Above code works but with all subcategories and I need only Main Category->Subcategory of Main->Subcategory of subcategory and thats all.