Get child categories when clicking on parent category

Grab the parent category $category = get_category_by_slug( 'category-name' );

Set whatever args you need for your post query, but make sure to include the child_of arg

$args = array(
    'type'                     => 'post',
    'child_of'                 => $category->term_id,
    'hierarchical'             => 1,
    'taxonomy'                 => 'category',
);

Get your subcategories based on these args $child_categories = get_categories($args);

You’ll get an associative array back, which you can loop over using foreach or whatever iterative function you want. If you want a more detailed solution to a specific problem in terms of a front-end application of this, feel free to update your question or post a comment.

Leave a Comment