How To Display An Old Post Message

Over the past several months i’ve noticed a common problem that websites and blogs have – Old, outdated content. It’s something that all of us are guilty of. Even on Art of Blog we have to make corrections to posts as WordPress is updated and products change.

The question is how do you, as someone in charge of a website, comb through the hundreds of posts you have to find outdated information?

After mulling the problem over a bit, I came up with a solution that is classy and can help you get some interaction on your website. My solution is to display a message at the top of old posts that warns your users that the information might be outdated, and encourages them to leave a comment if they find anything that needs to be corrected. I am considering old to be anything 180+ days.

The Code

Art of Blog is proudly powered by the Thesis theme, and as such we usually tailor our code to Thesis. This code is ready to be pasted into your custom_functions.php file.

Don’t have Thesis? That’s fine. You can still use this code with a bit of modification.

function oldPostMessage () {
	$posted = get_the_time('U');
	$current = current_time('timestamp');
	//Convert difference in seconds to days
	$diffTime = ($current - $posted) / (60*60*24);
	if($diffTime > 180){?>
		<div>Your HTML message will go here. This is an old post.</div><br>
	<?php }
}
add_action('thesis_hook_before_post','oldPostMessage');

Explanation Of The Code

One of my pet peeves is getting snippets of code and not understanding how or why they work – So, here’s a quick breakdown of what we’re doing with this code.

First we are pulling the timestamps for the day the post was published and the timestamp for the post was viewed.

Second, we use $diffTime to get the difference between the two time stamps. Curious what all that extra math is at the end of $diffTime? Well, a UNIX timestamp is a measurement of how many seconds a particular time is from January 1st, 1970. Because we are working in seconds and want to use days… we use all of that math to get to days :)

Last, we set up a simple if statement that just tests if the post is over 180 days old. If the post is over 180 days old, then our special message will be displayed alerting the visitor that the post might be outdated.

Have Questions?

Feel free to leave a question below if you have a question about implementation or want a more thorough explanation of what’s going on.

Image courtesy of Peregrino Will Reign

Sign Up Now for Free Updates and Exclusive Content:

Learn how to write killer content, get more traffic, make money, and more by entering your email below:

Written by Eric Binnion

Eric Binnion is a computer science student at Midwestern State University. When Eric is not online, he is usually volunteering in his community or enjoying time with his family. You can find Eric on Twitter.

Comments