How to show that a WordPress post is Old?

If you publish useful information on your WordPress Website/Blog, it is likely that some of your early website articles are out of date as technologies change quite rapidly over the years. In this article, I’ll show you how you can add a snippet of PHP tags to our WordPress themes to automatically add a small disclaimer or warning message to all Website Articles that are over ‘X’ years old.

These little snippets of code that we’ll add to the Website theme will allow us to display a message above our article’s content, but only on articles over a number of years old. We might only want to display this message on certain types of articles. We will also add a category filter to target only old posts from a particular section of the web blog.

[codebox 1]

The first line of code takes today’s year and minuses the year the article was published, which gives the article’s age in the $post_age variable. The snippet code then checks if the article’s age is over 2 years old and is in category ID 4. You could change this filter using many of the WordPress Conditional Tags.

A line of simple HTML flushes out a message to the user, explaining that the article is a few years old and therefore the article’s content may be outdated. We can use the WordPress tag to automatically enter the year the article was published.

Paste this code into your single.php template file above the tag.

CSS Styling

Once the snippet code is in place in the template file and the unformatted message is appearing on the correct posts, it can then be styled up with Cascading Style Sheets.

[codebox 2]

The old-post div is styled up with a textured grey background and plenty of padding around the edges. A warning icon is then added as a CSS background image on the paragraph element and the text indented with some left padding.

Does it work for you…If not, write a comment and we will help you out. We are also a WordPress Website Designers who can help you with your Website Design, Planning & Adding new features. You can contact us using the contact form.