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.

My categories look like this. Each has one or more posts

-Main Category
--Main Category Sub-category 1
--- 1 post here
--- 1 post here
--Main Category Sub-category 2
--- 1 post here
--- 1 post here
--- 1 post here
--Main Category Sub-category 3
--- 1 post here

When viewing Main Category archive page I only want the first post from each of the subcategories. So in my case I would only have 3 posts. Using twentythirteen theme. Don't want to change too much. I hope it can be done with query_posts.

        <?php /* The loop */ ?>
        <?php query_posts($query_string . '&??????'); ?>
        <?php while ( have_posts() ) : the_post(); ?>
            <?php get_template_part( 'content-category', get_post_format() ); ?>
        <?php endwhile; ?>
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

Don't use query_posts() use the WP_Query class instead... the link to the Codex will also reference all the appropriate query parameters.

The following would query the most recent post in a particular category, you will need to define the $category_id

// The Query
$query = new WP_Query( 'cat' => $category_id, 'posts_per_page' => 1 );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        get_template_part( 'content-category', get_post_format() );
    }
} 

/* Restore original Post Data */
wp_reset_postdata();
share|improve this answer
    
ok ... but that only returns one post. I was trying to return one post from each sub-category. –  ciprian Aug 26 '13 at 20:25
    
You didn't say how far along you were. You'll need to use get_categories() and run a foreach() loop. –  helgatheviking Aug 26 '13 at 21:07
add comment

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.