0

I'm trying to figure out a way to do a loop / archive page with posts organized by month. So where each page displays post from 1 month. Then the previous and next links would be labeled February 2011 and April 2011, for example.

I'm thinking it would possibly use the date.php file, but I feel like I may run into problems when paginating from one year to the next...

Anyone have any ideas, or documentation on where to look further?

Thanks!

1 Answer 1

3

I think this is not very hard to do. If you have currently have paginated date-based archives you can just disable paging when you are in a month overview:

add_action( 'pre_get_posts', 'wpse12983_pre_get_posts' );
function wpse12983_pre_get_posts( &$wp_query )
{
    if ( $wp_query->is_month() ) {
        $wp_query->set( 'nopaging', true );
    }
}

You can use get_month_link() to get links to the adjacent months. If you want to skip months without posts you can call get_previous_post() at the beginning of the loop and get_next_post() at the end (or you set the global $post variable yourself before you call these functions - they don't accept a post argument). Extract the dates from these posts and pass them to get_month_link().

3
  • how would you use this implementation? Feb 1, 2016 at 10:09
  • I tried it like this.It dint work which shows am clearly doing it wrong.Some help please @Jan Feb 1, 2016 at 10:15
  • @GotaloveCode: Best if you ask a separate question, with code showing what you have tried. And someone else can take a look, as it has been 5 years since I did any serious PHP work.
    – Jan Fabry
    Feb 3, 2016 at 13:59

Your Answer

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