The WordPress function wp_get_archives(‘type=postbypost’) displays a lovely list of posts, but won’t show the date of each post. This plugin adds each post’s date to those ‘postbypost’ lists, like so:
Usage
- Upload and activate the plugin
- Edit your theme, replacing
wp_get_archives('type=postbypost')
withif (function_exists('ard_get_archives')) ard_get_archives();
The function ard_get_archives();
replaces wp_get_archives('type=postbypost')
, meaning you don’t need to specify type=postbypost
. You can use all of the wp_get_archives() parameters except ‘type’ and ‘show_post_count’ (limit, format, before, and after). In addition, there’s a new parameter: show_post_date
, that you can use to hide the date, but the plugin will show the date by default.
show_post_date
(boolean) Display date of posts in an archive (1 – true) or do not (0 – false). For use with ard_get_archives(). Defaults to 1 (true).
Customizing the date
By default, the plugin displays the date as “(MM/DD/YYYY)”, but you can change this to use any standard PHP date characters by editing the plugin at the line:
$arc_date = date('m/d/Y', strtotime($arcresult->post_date)); // new
The date is wrapped in tags, so you can style the date independently of the link.
How does it work?
The plugin replaces the ‘postbypost’ part of the function wp_get_archives, and adds the date to $before. The relevant code is below. You can compare it to the corresponding lines in general-template.php.
} elseif ( ( 'postbypost' == $type ) || ('alpha' == $type) ) { ('alpha' == $type) ? $orderby = "post_title ASC " : $orderby = "post_date DESC "; $arcresults = $wpdb->get_results("SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit"); if ( $arcresults ) { $beforebefore = $before; // new foreach ( $arcresults as $arcresult ) { if ( $arcresult->post_date != '0000-00-00 00:00:00' ) { $url = get_permalink($arcresult); $arc_title = $arcresult->post_title; $arc_date = date('m/d/Y', strtotime($arcresult->post_date)); // new if ( $show_post_date ) // new $before = $beforebefore . '<span class="recentdate">' . $arc_date . '</span>'; // new if ( $arc_title ) $text = strip_tags(apply_filters('the_title', $arc_title)); else $text = $arcresult->ID; echo get_archives_link($url, $text, $format, $before, $after); } } } }
The lines ending in ‘// new’ are the only changes.
So you want the date to appear after the title? Edit the plugin to modify $after, instead:
} elseif ( ( 'postbypost' == $type ) || ('alpha' == $type) ) { ('alpha' == $type) ? $orderby = "post_title ASC " : $orderby = "post_date DESC "; $arcresults = $wpdb->get_results("SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit"); if ( $arcresults ) { $afterafter = $after; // new foreach ( $arcresults as $arcresult ) { if ( $arcresult->post_date != '0000-00-00 00:00:00' ) { $url = get_permalink($arcresult); $arc_title = $arcresult->post_title; $arc_date = date('j F Y', strtotime($arcresult->post_date)); // new if ( $show_post_date ) // new $after = ' (' . $arc_date . ')' . $afterafter; // new if ( $arc_title ) $text = strip_tags(apply_filters('the_title', $arc_title)); else $text = $arcresult->ID; echo get_archives_link($url, $text, $format, $before, $after); } } } }
Download
Get the files here: (Current version: 0.1 beta)