The archive contains older posts which may no longer reflect my current views.

# I've now got a provisional "On this day" feature at the bottom of the /Today page - just click the link to toggle them. I'm a bit happier with how it looks (I've tried to blend it in at the end of the posts) but may still tweak it. I'll write it up properly in the morning.

On this day...

In an episode of Clevercast Jonathan LaCour discusses freeing himself from Facebook by moving all his content from the network, as well as Instagram, to his own website. No small undertaking.

He mentions missing some features from Facebook such as the way it surfaces memories - inserting posts from the past into your feed. So, as part of the process he created an "On this day" page which lists all of his content, regardless of type or source, for the current day over the years.

It inspired me to do something similar but adding it to /Today rather than as a separate page. I looked to see if there was already a WordPress plugin to add this but, surprisingly, couldn't find one. So I set about writing my own solution.

How does it work?

To start I get the year of the first post to use as a base and assign this to a variable $count for use later. This is achieved with a post query then looking at the post_date of the oldest post. I could have just manually set this as 2008 but wanted to do it properly in case I reuse the code.

Then, for each year between then and last year (inclusive) run a custom query to get posts for the day of the year. The query is contained within a while loop while ($count < $today["year"]) and $count is incremented to stop at the right point.

The first time a post is found it displays the "On this day..." toggle but if no posts are found for the day in any year then nothing will be shown at all.

The toggle itself is a simple CSS trick using a hidden checkbox which, when checked by tapping on its label, shows the post links by use of the :checked pseudo-class:

#otdCheckbox:checked ~ .otdbody {

At the end, if $count == $today["year"] I close out the divs and it's ready. Just 37 lines of code (including whitespace) and a little CSS.

11 comments: click to read or leave your own Comments

# It's annoying when you know what you want to say but get stuck on how to say it. That's currently where I am with a post that's about 75% done but just needs that last bit (although "that last bit" is two thirds of the way through.)