I have found this code online but it shows all child categories when on parent category:
<?php
$this_category = get_category( $cat );
$child_categories = get_categories( array( 'child_of' => $this_category->cat_ID,'depth' => 1));
foreach( $child_categories as $category ) {
$category_url = get_category_link( $category->term_id );
$category_name = $category->cat_name; ?>
<div class="col3">
<img src="<?php echo z_taxonomy_image_url($category->term_id, array(365, 165)); ?>">
<h3><a href="<?php echo $category_url?>"><?php echo $category_name?></a></h3>
</div><?php
}
For example if I have
Parent -Child --Child 2 ---Child 3 Parent 2
When someone clicks on Child it should show only Child 2. This code now shows both Child 2 and Child 3 when someone clicks Child.