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.
Post Image The Easy Peasy Way
26 comments
page 1065
Html 5 Gallery
6 comments
page 1305
Post Image The Easy Peasy Way
26 comments
page 1065
Post Image The Easy Peasy Way
26 comments
page 1065
Using Your Own Url Shortener
4 comments
page 1190
Dont Mess With My Toot Toot
16 comments
page 599
Quick N Dirty Admin Login Screen
no comment
page 128
Using Your Own Url Shortener
4 comments
page 1190
Post Image The Easy Peasy Way
26 comments
page 1065
Theming Habari Vs Wordpress
13 comments
page 440
Are Child Themes The Best Option
15 comments
page 1262
Upload From Url
6 comments
page 326
Silence Is Golden
3 comments
page 213
Fun With Sidebar Tabs Styling
2 comments
page 336
Using Your Own Url Shortener
4 comments
page 1190
Wordpress 25 Exif Fields
12 comments
page 230
Html 5 Gallery
6 comments
page 1305
Dont Mess With My Toot Toot
16 comments
page 599
Dont Mess With My Toot Toot
16 comments
page 599
Converting Wordpress Themes To Habari
one comment
page 694
Wordpress 25 Exif Fields
12 comments
page 230
Fun With Sidebar Tabs Styling
2 comments
page 336
Using Your Own Url Shortener
4 comments
page 1190
Post Image The Easy Peasy Way
26 comments
page 1065
How To Add Sidebars To A Theme
10 comments
page 1053
Using Your Own Url Shortener
4 comments
page 1190
Html 5 Gallery
6 comments
page 1305
Dont Mess With My Toot Toot
16 comments
page 599
Quick N Dirty Replacement Text
no comment
page 122
1 query every 1971 seconds, updated 1 seconds ago.
(__)
`
WordPress and Blogger News and Tutorials – Blog Tipz
[...] Controlling Archives (WP-Fun) [...]