How to get a monthly post archive with navigation
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.
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
<?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('← %month %year'); ?></div> <div class="nav-next"><?php next_month_link('%month %year →'); ?></div> </nav> <?php else : ?> <?php twentyeleven_content_nav( 'nav-below' ); ?> <?php endif; ?>
This only works on monthly archive pages.
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.
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.
- The topic ‘How to get a monthly post archive with navigation’ is closed to new replies.