I have some code that displays the children categories on the screen but some parent categories do not have children if that is the case I don't want the section to be shown. does anybody know a way to do this? here is my current code to show the categories:
<div class="list-group products box">
<h4>Product Range</h4>
<?php $terms = get_the_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ){
$category_name = $term->name;
$parent_category_id = $term->term_id;
$categories=get_categories(array( 'parent' => $category_id ));
$children = get_terms( 'product_cat', array(
'parent' => $parent_category_id,
'hide_empty' => true
) );
foreach( $children as $subcat ){
?>
<a href="<?php echo get_term_link( $subcat->slug, 'product_cat' ); ?>" class="list-group-item"><?php echo $subcat->name; ?></a>
<?php } } ?>
</div>
When a parent as no children it just shows the H4 Product Range