I have found this code online but it shows all child categories when on parent category:

    $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>

For example if I have

--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.

1 Answer 1


'depth' is not a parameter for the function get_categories(); to get only direct child categories, try working with 'parent' instead of 'child_of'

https://developer.wordpress.org/reference/functions/get_categories/ https://developer.wordpress.org/reference/functions/get_terms/


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.