if it is for product categories, this function only needs the category id to know the childrens:
function cat_childs( $term_id = 0) {
$children = get_terms( array(
'child_of' => $term_id,
'taxonomy' => 'product_cat',
'hide_empty' => true,
'fields' => 'ids',
));
return ( $children );
}
can be called like this:
cat_childs(1234);
because it can return more than one that we can check first using
$a = cat_childs(1234);
echo '<pre>';
print_r($a);
if it is the case, we just need to do a foreach loop to get them all.
This is my code to make a menu with them all until the 3rd generation that uses the cat_childs() function to check if there is a need to the next loop.
$cat_args = array( 'orderby' => 'name', 'order' => 'asc', 'hide_empty' => true);
$cats = get_terms( 'product_cat', $cat_args );
foreach ($cats as $key => $cat):
if ($cat->parent == 0): ?>
<p><a href="<?php echo get_term_link($cat) ?>" style="color:red;"><?php echo $cat->name; ?></a></p><?php
if (cat_childs($cat->term_id)):
foreach ($cats as $key => $cat2):
if ($cat2->parent == $cat->term_id): ?>
<p><a href="<?php echo get_term_link($cat2) ?>" style="margin-left:20px; color:blue;"><?php echo $cat2->name; ?></a></p><?php
if (cat_childs($cat2->term_id)):
foreach ($cats as $key => $cat3):
if ($cat3->parent == $cat2->term_id): ?>
<p><a href="<?php echo get_term_link($cat3) ?>" style="margin-left:40px; color:green;"><?php echo $cat3->name; ?></a></p><?php
endif;
endforeach;
endif;
endif;
endforeach;
endif;
endif;
endforeach;