WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

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

Your Answer

 
discard

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