Put this in your theme’s functions.php: https://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('← %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.