Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using WordPress on developing a site and a self-made template. I got 1 page to display all categories with its post. This is what I want to do:

  • Main Category
    • subcategory1
      • post 1
      • subcategory1.2
        • post 1.2.1
        • post 1.2.2
    • subcategory2
    • subcategory3

I used wp_link_category() to display the category and I searched how to display category with post but the problem is: the post from subcategory 1.2 is also displayed on subcategory 1.

  • Main Category
    • subcategory1
      • post 1
      • post 1.2.1
      • post 1.2.2
      • subcategory1.2
        • post 1.2.1
        • post 1.2.2
    • subcategory2
    • subcategory3

How can I remove the post from subcategory 1.2 on subcategory 1?

Here's the code I copied and pasted from StackOverflow:

$categories =  get_categories('child_of=4');  
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>';
}

I hope you understand me.

share|improve this question

1 Answer 1

How can I remove the post from subcategory 1.2 on subcategory 1?

Use the 'category__in' argument parameter instead of the 'cat' parameter.

Here is a user function that does what you want.

/**
 * Category post list.
 *
 * An unordered list of category posts links. Posts in subcategories
 * are not listed in parent category. Skips categories with no posts.
 *
 * @param $parent_category The parent category to start with. Defaults to 0.
 */
function wpse_113987_category_post_list( $parent_category = 0 ) {

    $post_list_format = '<li><a href="%s">%s</a></li>';

    // Step through each category object.
    foreach ( get_categories( "child_of=$parent_category" )  as $category ) {

        $category_posts = get_posts( array(
            // Do not include posts in sub categories.
            'category__in'  => array( $category->term_id ),
        ) );

        // Skip categories with no posts.
        if ( empty( $category_posts ) )
            continue;

        echo "<h2>$category->name</h2>\n";
        echo "<ul>\n";

        // Step through each post object.
        foreach ( $category_posts as $post ) {
            printf( $post_list_format, get_permalink( $post->ID ), get_the_title( $post->ID ) );
        }

        echo "\n</ul><!-- end $category->name -->\n";
    }
}

To call it, use:

wpse_113987_category_post_list( 4 );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.