logo
  • Home
  • About
  • Plugins

Subscribe to Articles

Controlling Archives

Author: andrew Category: Uncategorized Tags: Snippets

Sunday
Mar 23, 2008

If you want to display a list of archive links in WordPress the way you do it is with the template tag: wp_get_archives. But what if you want a little more control?

Unlike bookmarks and categories there doesn’t seem to be a quick and easy way to retrieve an array of archive links as an array for later use. So what do you do if you want a list of archives exactly the same as that generated from the code below, but you want to insert something else into that list?

<?php wp_get_archives('type=monthly&limit=12'); ?>

WordPress has three functions that make it much easier to generate lists of archives manually:

  • get_day_link( $day , $month , $year );
  • get_month_link( $month , $year );
  • get_year_link( $year );

These functions each return the appropriate permalink for the date provided to them. All you need to do is to calculate the dates you want to include.

The code snippet below shows the links for this and the previous 11 months and adds a class (selected) to the list item if the page it is displayed on is an archive from the month and year selected.

for ($i = 0; $i < 12; $i++){
 
	$the_date = mktime(0, 0, 0, date("m")-$i, date("d"),   date("Y"));
	$year = date("Y", $the_date );
	$month = date("m", $the_date);
	$selected_class = '';
 
	if ( $wp_query->get('monthnum') == $month && $wp_query->get('year') == $year ){
		$selected_class = ' class="selected" ';
	}
 
	echo '<li'.$selected_class.'>';
	echo '<a href="'.get_month_link($year, $month).'">'.date('F Y',$the_date).'</a>';
	echo '</li>';
 
}

This can be amended in a variety of ways to add extra functionality to a theme, including paging, listing posts inline, and even combining with Javascript to produce a dynamic menu of years, months and days.


Share:
image image image image image

Comments


Microformat Pingbacks
New Plugin: Fun with Microformat Pingbacks

Recommended

wordpress unlimited

Archives

  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007

Tags

2.7 2.8 Admin Advertising Air Blogging blogs Cache Cliche Coding Comments Content Types CSS Curry Death development Disqus Features Habari image Interview Licensing Modes Monday Poll New Features Organisation Personal Platform Plugin Plugin-Practices plugins Plugin update Poll premium Readers Reviews Search Simplification Snippets Spam Themery Themes tools User Interface Widgets WLTC

Copyright 2009 Fun with WordPress - All Rights reserved.

Wordpress theme by: WPUnlimited