How to get a monthly post archive with navigation (6 posts)

  1. 1BJK903
    Posted 3 years ago #

    Hi all,

    I want to have a monthly post archive page on my website, which gives me the ability to navigate between the months with a simple arrow or something like that. So I have seen the Smart Archives Reloaded plugin, but that does not have a navigation like the one I want.

    Would appreciate it if someone can find a solution.

  2. keesiemeijer
    Posted 3 years ago #

    Put this in your theme's functions.php: http://pastebin.com/0jCWtDyD
    Now you can use the functions next_month_link() and previous_month_link() in your theme's archive template file (archive.php).

    These functions will show links to the next or previous month that has posts in it with the default link text: "Next Month" and "Previous Month". You can overide the default link text with:

    <?php next_month_link("How About Next Month"); ?>

    If you want the year and month in the link text you can use %year and %month

    <?php previous_month_link('View previous posts from: %month %year'); ?>

    this will make the previous month link text: "View previous posts from: September 2012"

    Example usage in the default theme Twenty Eleven:

    <?php if(is_archive() && is_month()) : ?>
    <nav id="nav-below">
    <h3 class="assistive-text"><?php _e( 'Montly Posts navigation', 'twentyeleven' ); ?></h3>
    <div class="nav-previous"><?php previous_month_link('&larr; %month %year'); ?></div>
    <div class="nav-next"><?php next_month_link('%month %year &rarr;'); ?></div>
    <?php else : ?>
    <?php twentyeleven_content_nav( 'nav-below' ); ?>
    <?php endif; ?>

    This only works on monthly archive pages.

  3. wonderchuck
    Posted 3 years ago #

    Thank you, this was an absolute lifesaver. My company is in the process of converting our Movable Type blogs to WP, and Movable Type had prev/next month navigation in the monthly archives. I was pulling my hair out trying to figure out a way to implement the same functionality in WP, and I stumbled across this. A million thanks again.

  4. keesiemeijer
    Posted 3 years ago #

    You're welcome. I'm glad it works for you :-)
    I'm thinking of making this into a plugin.

    I forgot to mention that this (code) requires PHP 5.3 to work.

  5. 1BJK903
    Posted 3 years ago #

    Bedankt Keesie :)

  6. Li-An
    Posted 3 years ago #

    Thanks a lot !

Topic Closed

This topic has been closed to new replies.

About this Topic