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 was wondering if there was any way to make a custom loop that divides posts into months. I would use it on the Archive page to list all posts within a given category with a title that indicated what month it was posted.

Like this:

January:

post1 post2 post3

February:

post4 post5 post6

Any help would be appreciated. Thanks!

share|improve this question

migrated from stackoverflow.com Jul 4 '11 at 14:47

This question came from our site for professional and enthusiast programmers.

add comment

1 Answer

you could do this in any loop by checking the current post's published month, and printing it when it changes:

<?php
// set current month to that of first post, and print it.
$current_month = get_the_time('F');
echo $current_month;

while (have_posts()) : the_post();

    // check each subsequent post to see if the month is the same
    // or has changed and needs to be printed:
    $this_month = get_the_time('F');
    if( $this_month!=$current_month ):
        $current_month = $this_month;
        echo $current_month;
    endif;

    // output data for the post
    the_title();

endwhile;

?>
share|improve this answer
add comment

Your Answer

 
discard

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