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 need to display all the months that have active posts, and inside each month I need to display at least 5 posts that are tied to each month.. the HTML will look like this:

News items
<h6>May</h6>
<ul>
    <li><a href="#">Souvlaki ignitus carborundum</a></li>
    <li><a href="#">Defacto lingo est igpay atinlay</a></li>
    <li><a href="#">Quote meon an estimate</a></li>
    <li><a href="#">Souvlaki ignitus carborundum</a></li>
    <li><a href="#">Defacto lingo est igpay atinlay</a></li>
    <li><a href="#">Quote meon an estimate</a></li>
</ul>
<h6>April:</h6>
<ul>
    <li><a href="#">Sic tempus fugit esperanto hiccup</a></li>
    <li><a href="#">Epsum factorial non deposit</a></li>
</ul>

Not sure how I can go about this or what function to use.. any help/direction would be appreciated.

share|improve this question
    
can you give more details about the 'at least 5 posts'? –  Michael Jul 10 '12 at 21:55
    
i need to display the latest 5 posts from each month –  user990717 Jul 10 '12 at 22:59
add comment

1 Answer

up vote 3 down vote accepted

one possibility is to run all posts through the loop and only output the month once per new month:

<?php       
$counter = 0;
$ref_month = '';
$monthly = new WP_Query(array('posts_per_page' => -1));
if( $monthly->have_posts() ) : while( $monthly->have_posts() ) : $monthly->the_post();

    if( get_the_date('mY') != $ref_month ) { 
        if( $ref_month ) echo "\n".'</ul>';
        echo "\n".'<h6>'.get_the_date('F').'</h6>';
        echo "\n".'<ul>';
        $ref_month = get_the_date('mY');
        $counter = 0;
    }

if( $counter++ < 5 ) echo "\n".'   <li><a href='.get_permalink($post->ID).'>'.get_the_title($post->ID).'</a></li>';

endwhile; 
echo "\n".'</ul>';
endif; 
?>
share|improve this answer
    
I thought of doing this but was looking for a more 'native' solution.. nothing out of the box that can generate this automatically? –  user990717 Jul 13 '12 at 22:22
    
Here's another response on So: stackoverflow.com/questions/11479176/… –  user990717 Jul 15 '12 at 5:27
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.