0

I'm trying to show all posts posted under the month, so

2012

December

post 1
post 2

November

post 1
post 2

The posts are to appear under my custom taxonomy template, so taxonomy-pubyear.php and I'm able to retrive all the posts for that term using the basic loop. If I add get_the_year('F') in the loop then the same month will be displayed repeatedly but I need it displayed once and have all the corresponding posts show up under it, any help with this would be appreciated.

Note: If I have post published under the same day, month and year then the above get_the_year('F') will show the month and display those posts below it, i want that functionality but without having to use the same published date.

0

Save the current month in a variable and check it for each post, only output it when it changes:

$current_month = '';

while( have_posts() ):
    the_post();

    $this_month = get_the_time( 'F' );

    if( $this_month != $current_month ){
        $current_month = $this_month;
        echo $current_month;
    }

    the_title();

endwhile;
  • That worked! Thanks a million. Just out of curiosity, why not use the get_the_date() function instead? – Beto Jan 17 '13 at 3:30
  • 1
    you could use get_the_date here, it would work the same. I'm in the habit of using get_the_time because it's a bit more flexible, as you can also pass a post id where get_the_date will only work with the global $post object. – Milo Jan 17 '13 at 3:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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