"Sidebar Widgets" and drag and drop the widget wherever you want to show it.
Changelog
v0.7.2 = Bugfixing release. Changes contributed by Mike Koepke.
Ordering in SQL statement wasn't specify which column to order on
Added post_type qualifier to SQL statement as pages would be pulled as well
Added new year subtitle display option to hide the year sublist text
PostExcerpt and Date weren't working correctly
Code cleanup.
Cosmetic changes in output
v0.7.1 = Bugfixing release. Changes contributed by Mike Koepke.
New options weren't initilized correctly when upgrading.
Control Screen cleaned up.
v.0.7 = All displayed in a single list with yearly sublists.
NEW Feature: display date and/or excerpt.
NEW Feature: added CSS markup to allow maximum flexibility on display.
v.0.6.3 = Fixed a bug when calling get_old_posts().
v.0.6.2 = Fixed a bug when having & in post's title. Isolated repetitive code in its own function. Changes contributed by Mike Koepke.
v.0.6.1 = Fixed bug when having quotes in post's title. Thanks to Mike Koepke for bug repporting and helping with fixing.
v0.6 = NEW Feature: added 2 new modes, last X years and since year X.
Corrected bug in $start_ago and $end_ago.
v0.5.2 = XHTML valid. Luís Pérez (aka Cinéfilo)
v0.5.1 = Now the $yearsago parameter works.
v0.5.01 = Now the limit option works, for real ;)
v0.5 = First release. I decided to start on version 0.5 because of preserving version relation with "One year ago" plugin.
Thanks
Thanks to Borja Fernandez, Chris Goringe and Sven Weidauer for writing their plugins and to Luis P�rez and Mike Koepke for their contributions.
*/
function widget_atd_init()
{
if (!function_exists('register_sidebar_widget') || !function_exists('register_widget_control'))
return;
function around_this_date( $daysbefore, $daysafter, $mode, $yearsago, $lastxyears, $sinceyear, $limit, $none, $showyear, $showdate,
$dateformat, $showexcerpt)
{
$outputlist = ''; // empty the 'outputlist' string
$outputlist .= '
';
switch ($mode)
{
case 1: // "classic" mode
$start_ago = (365*$yearsago)+$daysbefore;
$end_ago = (365*$yearsago)-$daysafter;
$year = date("Y")-$yearsago;
$liststart = '';
$listend = ' ';
if ($showyear)
{
$liststart .= $year;
}
$outputlist .= $liststart;
$outputlist .= get_old_posts( $start_ago, $end_ago, $limit, $none, $showyear, $showdate, $dateformat, $showexcerpt);
$outputlist .= $listend;
break;
case 2: // last x years mode
for($year = 1; $year <= $lastxyears; $year++)
{
$start_ago = (365*$year)+$daysbefore;
$end_ago = (365*$year)-$daysafter;
$liststart = '';
$listend = ' ';
if ($showyear)
{
$liststart .= (date("Y")-$year);
}
$outputlist .= $liststart;
$outputlist .= get_old_posts( $start_ago, $end_ago, $limit, $none, $showyear, $showdate, $dateformat, $showexcerpt);
$outputlist .= $listend;
}
break;
case 3: // since year x mode
for($year = 1; $year <= (date("Y")-$sinceyear); $year++)
{
$start_ago = (365*$year)+$daysbefore;
$end_ago = (365*$year)-$daysafter;
$liststart = '';
$listend = ' ';
if ($showyear)
{
$liststart .= (date("Y")-$year);
}
$outputlist .= $liststart;
$outputlist .= get_old_posts( $start_ago, $end_ago, $limit, $none, $showyear, $showdate, $dateformat, $showexcerpt);
$outputlist .= $listend;
}
break;
}
$outputlist .= ' ';
echo "$outputlist";
}
function get_old_posts( $start_ago, $end_ago, $limit, $none, $showyear, $showdate, $dateformat, $showexcerpt )
{
global $wpdb;
global $wp_version;
$q = "SELECT ID, post_title, post_date, post_excerpt, post_content FROM $wpdb->posts ";
$q .= " WHERE post_status = 'publish'";
if (version_compare($wp_version, '2.1', '>='))
$q .= " AND post_type = 'post'";
$q .= " AND (( TO_DAYS( NOW() ) - TO_DAYS( post_date ) ) BETWEEN " . $end_ago . " AND " . $start_ago . ")";
$q .= " ORDER BY post_date ASC";
$q .= " LIMIT $limit";
$entries = $wpdb->get_results($q);
$output = ''; // empty the 'output' string
if ($showyear)
$output .= '';
if (!empty($entries))
{
foreach ($entries as $entry)
{
$title = str_replace('"', '',$entry->post_title);
$title = htmlspecialchars($title);
if($showdate)
$postdate = ' - ' .mysql2date($dateformat,$entry->post_date). ' ';
else
$postdate = '';
if($showexcerpt)
{
if (empty($entry->post_excerpt))
{
$entry->post_excerpt = explode(" ",strrev(substr(strip_tags($entry->post_content), 0, 100)),2);
$entry->post_excerpt = strrev($entry->post_excerpt[1]);
$entry->post_excerpt.= " [...]";
}
$postexcerpt = htmlspecialchars($entry->post_excerpt);
$postexcerpt = '' .$postexcerpt. ' ';
}
else
$postexcerpt = '';
$classes = ' atd-y'. mysql2date('Y',$entry->post_date). ' atd-m'. mysql2date('m',$entry->post_date). ' atd-d' .
mysql2date('d',$entry->post_date) . ' atd-' . mysql2date('Ymd',$entry->post_date) . '';
$output .= '' . htmlspecialchars($entry->post_title) . ' ' .
$postdate . ' ' . $postexcerpt . ' ';
}
}
else
{
$output .= '' . $none. ' ';
}
if ($showyear)
$output .= ' ';
return $output;
}
function widget_atd_options()
{
$defaults = array(
'title' => 'This week last year...',
'daysbefore' => 3,
'daysafter' => 3,
'mode' => 1,
'yearsago' => 1,
'lastxyears' => 1,
'sinceyear' => 2006,
'limit' => 4,
'none' => 'none',
'showyear' => true,
'showdate' => false,
'dateformat' => 'F j',
'showexcerpt' => false
);
$options = (array) get_option('widget_atd');
foreach ( $defaults as $key => $value )
if ( !isset($options[$key]) )
$options[$key] = $defaults[$key];
return $options;
}
function widget_atd_display( $args )
{
extract( $args );
$options = widget_atd_options();
echo $before_widget . $before_title . $options['title'] . $after_title;
around_this_date( $options['daysbefore'], $options['daysafter'], $options['mode'], $options['yearsago'], $options['lastxyears'],
$options['sinceyear'], $options['limit'], $options['none'], $options['showyear'], $options['showdate'], $options['dateformat'],
$options['showexcerpt'] );
echo $after_widget;
}
function widget_atd_control()
{
$options = $newoptions = widget_atd_options();
if ($_POST['atd-submit'])
{
$newoptions['title'] = strip_tags(stripslashes($_POST['atd-title']));
$newoptions['daysbefore'] = strip_tags(stripslashes($_POST['atd-daysbefore']));
$newoptions['daysafter'] = strip_tags(stripslashes($_POST['atd-daysafter']));
$newoptions['mode'] = strip_tags(stripslashes($_POST['atd-mode']));
$newoptions['yearsago'] = strip_tags(stripslashes($_POST['atd-yearsago']));
$newoptions['sinceyear'] = strip_tags(stripslashes($_POST['atd-sinceyear']));
$newoptions['lastxyears'] = strip_tags(stripslashes($_POST['atd-lastxyears']));
$newoptions['limit'] = strip_tags(stripslashes($_POST['atd-limit']));
$newoptions['none'] = strip_tags(stripslashes($_POST['atd-none']));
$newoptions['showyear'] = isset($_POST['atd-showyear']);
$newoptions['showdate'] = isset($_POST['atd-showdate']);
$newoptions['dateformat'] = strip_tags(stripslashes($_POST['atd-dateformat']));
$newoptions['showexcerpt'] = isset($_POST['atd-showexcerpt']);
}
if ( $options != $newoptions )
{
$options = $newoptions;
update_option('widget_atd', $options);
}
$title = attribute_escape($options['title']);
$daysbefore = (int) $options['daysbefore'];
$daysafter = (int) $options['daysafter'];
$mode = (int) $options['mode'];
$yearsago = (int) $options['yearsago'];
$sinceyear = (int) $options['sinceyear'];
$lastxyears = (int) $options['lastxyears'];
$limit = (int) $options['limit'];
$none = attribute_escape($options['none']);
$showyear = ($options['showyear'] ? 'checked="checked"' : '');
$showdate = ($options['showdate'] ? 'checked="checked"' : '');
$dateformat = attribute_escape($options['dateformat']);
$showexcerpt = ($options['showexcerpt'] ? 'checked="checked"' : '');
?>
Basic options:
Title:
Days before:
Days after:
Limit of entries:
Text when no entries:
Select Mode: $i"; ?>
mode 1: get posts around this date from X years ago.
mode 2: get posts around this date from the last X years.
mode 3: get posts around this date since year X.
Mode Options:
Advanced options:
Show excerpt:
Show year subtitle:
Show post date:
Date format: See PHP Date manual