frederik's blog

Music (5) Projects (2) Snippets (8) Technology (5) Thoughts (9)

Speak×

Highlight old WordPress posts

!
/ (last edited)

Some WordPress posts have a very short lifespan in terms of relevance.

Two days ago I wrote a short article about my Web Speech API experiments and I’m sure that the information contained will be outdated within a few months.

I decided it might be a good idea to emphasize old posts or add some sort of hint for all those readers coming late to the party.

This is the snippet I use in my themes’ single.php to highlight posts after a certain time. I think in my case 90 days is reasonable.

/* this goes into the theme's header.php - I put it before the post date */

<?php if (time() - strtotime($posts[0]->post_date_gmt) > (90 * 24 * 60 * 60)) {
    if (in_category(array('technology', 'linux-unix', 'snippets', 'links'))) {
        echo "<span class=\"frjk-old-post\" title=\"This post is older than 90 days. It might be outdated.\">!</span>";
    }
} ?>

I added a condition to make sure that only posts filed under Technology, Snippets, Linux/Unix and Links are emphasized. Posts from the remaining categories are not as likely to “expire”.

The time() function returns the current time in “unix time” (seconds). The post date is “substracted” from the current time and if the resulting number of seconds is greater than 90 * 24 * 60 * 60 (90 days), we display a hint. In my case I added an exclamation mark. If the user hovers over it, he’ll see what’s up.

Bonus: Highlight fresh WordPress posts

We can easily turn things around and emphasize new blog posts instead. Adjust the number of days to fit your needs.

<php if (time() - strtotime($posts[0]->post_date_gmt) < (7 * 24 * 60 * 60)) {
    echo "<span class=\"frjk-fresh-post\" title=\"This is a fresh post, not older than 7 days.\">!</span>";
} ?>

In this case the category doesn’t matter.

Bonus: Highlight recently updated WordPress posts

Now we’re getting all fancy!

We can emphasize only those posts that have been updated recently by using this snippet. Instead of post_date_gmt we use post_modified_gmt.

Again, you can change the number of days if you like.

<php if (time() - strtotime($posts[0]->post_modified_gmt) > (30 * 24 * 60 * 60)) {
    echo "<span class=\"frjk-improved-post\" title=\"This post has been updated recently.\">!</span>";
} ?>