Spend Little Time Learning How to Display a Message on Old WordPress Posts

How to Display a Message on Old WordPress Posts

With technology changing rapidly, blog posts may be outdated after a few years. These older posts can drive traffic to your blog, so you don’t want to remove them. Rather than spending the time finding old, outdated posts and adding a disclaimer to each post, you can add PHP code to your WordPress theme to display a notice on posts that are over a certain age.

Once you add the code, a box with a message, stating that the post is over a certain age in years will be displayed above the post. You can also add a category filter so this message is only displayed on posts within a certain category such as tutorials or web design, which can become outdated quickly.

PHP Code To Add To A WordPress Theme To Denote Older Posts

The following code should be added to your single.php file just before the content of the post:

< ?php
$post_age = date('Y') - get_the_time('Y');
if($post_age > 2 && in_category('4') ) { ?>
<div class="old-post">
<p><strong>This post was originally published in < ?php the_time('d/m/Y'); ?></strong><br />
The tips and techniques explained may be outdated.</p> 
< ?php } ?>

In the above code, the $post_age variable gives the age of the post by taking today’s year and subtracting the year the blog post was published. The script then checks to see if the post is over the age variable and in the correct category. Incidentally, these can be changed using Conditional Tags within WordPress.

If the blog post meets both criteria, age and category, a message is displayed in a box above the post. The message will inform visitors that the post is older than X years and content could be outdated. You can change the text of the message to suit your needs if desired. If you want to display the full date the post was published, add it within the code.

Adding Style With CSS

Once you have verified that the code is working properly within the template file and the message is being displayed on the specified posts, you can then use CSS to change the style so it matches your blog layout:

.old-post { 
        margin: 0 0 20px 0; 
        padding: 18px 20px 0px 25px;
        background: #FFFFE0;
        border: 1px dashed #EE9A0A;
.old-post p {
        background: url(http://aux.iconpedia.net/uploads/1381126273.png) left no-repeat; 
        padding: 0 0 0 70px;
        color: #717171;

This following code gives a yellow background with padding around the edges of the message to grab the reader’s attention. It also adds a warning icon as the background image for the paragraph element. The text is indented with padding on the left to further offset the post to grab the reader’s attention:

You can change the padding, background or icon within this CSS code to match your website’s color scheme and background.

Published by

Mike A.

Mike A. is a web developer living in Southern California. He has written many articles and tutorials on Web Development.

One thought on “Spend Little Time Learning How to Display a Message on Old WordPress Posts”

Comments are closed.