March 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]
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.
[php]
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 '
';
echo ''.date('F Y',$the_date).'';
echo '
';
}
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.
Wordpress Chat
one comment
page 1308
Html 5 Gallery
6 comments
page 1305
Html 5 Gallery
6 comments
page 1305
Silence Is Golden
3 comments
page 213
Questions About Habari For Wordpress Users
6 comments
page 424
Theming Habari Vs Wordpress
13 comments
page 440
My Experience Of Flexx
4 comments
page 1026
Plugin Update Fun With Photo Data 2
one comment
page 815
Post Image The Easy Peasy Way
26 comments
page 1065
Categories Vs Tags Either Neither Or Both
12 comments
page 7
Gaining Benefits From Plugins
8 comments
page 1167
Beware Wp Cache
8 comments
page 1310
Six Million Ways To Die Choose One
14 comments
page 1128
Post Image The Easy Peasy Way
26 comments
page 1065
Wordpress Chat
one comment
page 1308
Post Image The Easy Peasy Way
26 comments
page 1065
Wordpress Chat
one comment
page 1308
Beware Wp Cache
8 comments
page 1310
Wp Polls Reviewed
one comment
page 58
Fun With Photo Data
12 comments
page 330
Fun With Sidebar Tabs Styling
2 comments
page 336
Poll Daddy Reviewed
2 comments
page 42
Html 5 Gallery
6 comments
page 1305
Wp Polls Reviewed
one comment
page 58
Using Your Own Url Shortener
4 comments
page 1190
Html 5 Gallery
6 comments
page 1305
Html 5 Gallery
6 comments
page 1305
Fun With Plugins
27 comments
page 14
Wordpress 25 Exif Fields
12 comments
page 230
Html 5 Gallery
6 comments
page 1305
Html 5 Gallery
6 comments
page 1305
Html 5 Gallery
6 comments
page 1305
Quick N Dirty Replacement Text
no comment
page 122
Beware Wp Cache
8 comments
page 1310
Theming Habari Vs Wordpress
13 comments
page 440
updated 1 seconds ago
[...] Controlling Archives (WP-Fun) [...]