Support » Fixing WordPress » Query showing subcategory of post's category

  • I want to show the subcategory of the category related to each post in my website,
    I mean when the user visit any post in my website, I want him to see at the end of the post the following:

    • SubCat1
      • Post 1
      • Post 2
    • SubCat2
      • Post 1
      • Post 2
    • SubCat3
      • Post 1
      • Post 2

    I managed to find this code:

    $categories =  get_categories('child_of=31');
    foreach  ($categories as $category) {
        //Display the sub category information using $category values like $category->cat_name
        echo '<h2>'.$category->name.'</h2>';
        echo '<ul>';
        foreach (get_posts('cat='.$category->term_id) as $post) {
            setup_postdata( $post );
            echo '<li><a href="'.get_permalink($post->ID).'">'.get_the_title().'</a></li>';
        echo '</ul>';

    but this is for category with id= 31 , I want to make it variable.
    any help please ?

  • The topic ‘Query showing subcategory of post's category’ is closed to new replies.