Build your own "On This Day" page for WordPress
I blog. A lot. Too much really. One of the things I like to do is see what I was rambling on about this time last year. And the year before that. And so on.
So, here's my On This Day page and here's how I built it.
WARNING Extremely quick and dirty code ahead!
This allows you to add a shortcode like [ edent_on_this_day ]
to a page and have it auto generate a list of posts you published on this day in previous years. You may need to exclude that page from your cache.
Add these functions to your theme or to a new plugin:
PHPfunction edent_on_this_day_shortcode() {
$today = getdate();
$args = array(
'date_query' => array(
array(
'month' => $today['mon'],
'day' => $today['mday'],
),
),
);
$query = new WP_Query( $args );
$posts = $query->get_posts();
$today = getdate();
$pubDate = date("D, d M Y") . " 00:00:00 GMT";
$output = "<h2>From the " . date("jS \of F") . " archives</h2>";
$output .= "<ul>";
foreach($posts as $post) {
$title = $post->post_title;
$id = $post->ID;
$link = get_permalink($id);
$date = $post->post_date;
$postDate = date("D, d M Y") . " " . date("h:i:s O", strtotime($date));
// $thumb = get_the_post_thumbnail($id, 'full');
$archive = "" . date("Y", strtotime($date)) . ": ";
// Only add an item if it is before *this year*
if (
intval(date("Y", strtotime($date))) < intval($today['year'])
) {
$output .= '<li><a href="' . htmlspecialchars($link) .'">';
$output .= html_entity_decode($archive . $title) . '</a></li>';
}
}
$output .= "</ul>";
return $output;
}
// Set up the shortcode
function edent_on_this_day_shortcode_init() {
add_shortcode( 'edent_on_this_day', 'edent_on_this_day_shortcode' );
}
add_action( 'init', 'edent_on_this_day_shortcode_init' );
I really can't be bothered to deal with WordPress's complicated plugin publishing system - so feel free to copy the above with or without attribution.
I originally built this as an RSS feed - but decided recently that a regular HTML page was more useful. If you spot any bugs, you can contribute on GitHub.
Enjoy!