I need to know what is wrong with my code here , i am trying to get all subcategories and posts related to it

Viewed 76

now i am tying to get all the posts related to it's subcategory I am using this code to check if the taxonomy is parent or not but the problem that it doesn't show me the subcategories i get only posts so

How can i get all subcategories and the posts related to it

    $term = get_queried_object();
    if($term->post_parent !=0 ){ 

    //  echo 'has parent'; //this post category has child
    $term_id = $term->term_id;
    $taxonomy_name = $term->taxonomy;
    $termchildren = get_term_children( $term_id, $taxonomy_name );
     // echo    $postcat ;
     foreach ($termchildren  as $child) {
    $term = get_term_by( 'id', $child, $taxonomy_name );
             ?>
             // get data of the subcategory


             <?php }?>



     <?php   }
     else{
    while(have_posts()):the_post();
       // get the subcategory posts

<?php endwhile ?>
<?php  }?>
0 Answers
Related