WordPress.org

Ready to get started?Download WordPress

Forums

Query showing subcategory of post's category (1 post)

  1. alaadesign
    Member
    Posted 1 year ago #

    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 ?

Topic Closed

This topic has been closed to new replies.

About this Topic