1

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

3
  • Note that shortcodes should not echo anything because it can break many things, e.g. the (JSON) output of a REST API endpoint (which for example, causes the block editor to fail in saving a post). So instead of echoing the output, use a variable and store the output into that variable, then return that variable, i.e. return $your_variable;.
    – Sally CJ
    Jan 14 at 1:45
  • thanks @SallyCJ i will keep that in mind also can you please point me towards a solution for my question. Jan 14 at 1:49
  • Yes, maybe, but can you fix the punctuation in your question, e.g. use a comma when there should be one. Just use the Edit link to edit your post. And can you clarify the question, because it's not clear to me - do you just want to show a list of categories with links to their archive page? Also, keep in mind that a question should be limited to one question only - if you have multiple questions, don't ask them all at once in a single post.
    – Sally CJ
    Jan 14 at 2:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.