Site icon Hip-Hop Website Design and Development

Show categories then when clicked on a category list all sub category and when clicked show all posts in that sub category

Show categories then when clicked on a category list all sub category and when clicked show all posts in that sub category

function cc_showcountries_all_cats() { 
    $taxonomy = 'countries';

$terms = get_terms( $taxonomy ); //Get all the terms
 
foreach ($terms as $term) { //Cycle through terms, one at a time
 
// Check and see if the term is a top-level parent. If so, display it.
$parent = $term->parent;
 if ( $parent=='0' ) {
 
    $term_id = $term->term_id; //Define the term ID
    $term_link = get_term_link( $term, $taxonomy ); //Get the link to the archive page for that term
    $term_name = $term->name;
    echo '<h2><a class="ccats" href="' . $term_link . '"><span class="label">' . $term_name . '</span></a></h2>';   
} }
    
} 
// register shortcode
add_shortcode('showcountries', 'cc_showcountries_all_cats'); 

currently i am using this short code function to show all the categories what i want is when its a category is clicked it should all the child categories to that category then when that child is clicked it should show all the posts.
please help