I use this blog’s Nerd stuff category for IT and technology related posts. But the oldest posts date back more than 10 years, and technology changes fast.
So I decided to add a warning to messages older than 3 years in that category. To do so, I added this code snippet to the functions.php file in the theme folder:
<?php add_filter('the_content', 'post_age'); function post_age($content) { if (is_category('nerd-stuff') || ((is_home() || is_single()) && in_category('nerd-stuff'))) { # Minimum post age to display the message. # 3 years seems reasonable for technology. $min_yrs = 3; $min_sec = $min_yrs * 365 * 24 * 60 * 60; # Post age in seconds. $post_sec = date('U') - get_post_time(); if ($post_sec > $min_sec) { # Post age in years. $post_yrs = round($post_sec / 60 / 60 / 24 / 365); # Prepend the warning to the content. return "<div style='border:1px dashed #cc0000; padding:25px; margin-bottom:25px; text-align:center; font-weight:bold; color:#cc0000;'>Please keep in mind that this post is about $post_yrs years old.<br>Technology may have changed in the meantime.</div>" . $content; } # Post is not old enough for a warning. return $content; } # We are not on the category page, the home page, or a single post page. return $content; }
For the category, I used the ‘slug’ in the code above, but I could also have taken the category name or the category ID.
And I didn’t actually add this code to the theme’s functions.php. I first created a child theme as described here, and then added the code to the child theme’s functions.php. This way, I don’t lose my code if and when the theme is updated.
For the result, you can just browse the Nerd stuff category.