wordpress monthly navigation

Date: 25th October 2007 at 4:11 pm | Filed under: development, scripts, wordpress | Author: Sam Burdge

This article explains how to create a monthly navigation system for one or more categories in wordpress, or on the home page itself. By this I mean that on the first page it would display only posts from the current month. The next and previous page links would be replaced by next and previous month links.

To display the monthly content it is first necessary to create a custom query. This should be done before the loop begins. Here is an example of the custom query php:

//custom query code
//check for cmonth added by the monthly nav
$cmonth = $_GET["cmonth"];
if($cmonth==""){$cmonth = 0;}
//get the month minus the offset of cmonth
$current_month = date('m', strtotime($date.'-'.$cmonth . 'months' ));
//get the year minus the offset of cmonth
$current_year = date('Y', strtotime($date.'-'.$cmonth . 'months' ));
//make the query
query_posts("monthnum=$current_month&year=$current_year&order=DESC");

The next step is to run the loop as normal. You can also add in a heading for the current month and year being displayed as shown below:

//set a parameter i as 1
//this helps to identify the first post
$i=1
//loop starts
if (have_posts()) : while (have_posts()) : the_post(); ?>
//display the month and year at the top of the page
<?php if($i=='1'){echo '
<h1>'; the_time('F Y'); echo '</h1>
 
';} ?>
<!--post title -->
<h2><?php the_title(); ?></h2>
 
//post content
<?php the_content(__('Read more'));?>
//increase i through the loop
<?php $i++; ?>
<?php endwhile; else: ?>
<?php _e('Sorry, no posts matched your criteria.'); ?>
//end the loop
<?php endif; ?>

The last, and trickiest part is the navigation, which changes the $cmonth parameter in th URL string:

//set values for the next and previous cmonth
$cmonth_num_plus = $cmonth + 1;
$cmonth_num_minus = $cmonth - 1;
//set up a way to check the first day
$cmonth_check = $current_month-$cmonth;
//set the date that your blog starts from
//there should be no blog posts earlier than this date
//in this example the start date is sept 2007
//the month is represented by a number
$start_month = 8;
$start_year = 2007;
//set the divider between the links
$divider = ' &#124; ';
//if the previous month exists display the 'previous' link
if($current_year >= $start_year && $cmonth_check>$start_month){
echo '<a href="'.get_bloginfo('url').'/?cmonth=';
echo $cmonth_num_plus.'">&laquo; Previous Month</a>';
}
//if there is a 'previous' and 'next' display the divider
if($current_year >= $start_year && $cmonth_check>$start_month && $cmonth>0)
{echo $divider;}
//if its not the current month display the 'next' link
if($cmonth>0){
echo '<a href="'.get_bloginfo('url').'/?cmonth=';
echo $cmonth_num_minus.'">Next Month &raquo;</a>';
}

If you have any questions about this please leave a comment.

1 Response to “wordpress monthly navigation”

  • Comment by hanslukas
    Date: May 12th, 2008 at 11:09 pm

    hi…
    it’s just what i was searching…

    I tried to use your solution pasting it on mi index.php, but don’t work…
    where my error please?

    Sorry for my english,I thank you
    regards

Leave a Comment