15 September, 2013 β€” Development, Snippets, WordPress β€”

Display A Message On Older Posts

When blog posts get old, sometimes the information in them is not as accurate as they were at the time of writing. This can be misleading and can cause confusion to the reader.

This simple bit of code when either placed in your themes funcions.php file or a functionality plugin will display a message at the top of any posts that are older than 1 year. You can customise the message in the code below and add some styles to your style.css file. Once done upload your file WordPress will automatically take care of the rest.

// display a message on older posts
function tb_older_post_message () {
    $posted = get_the_time('U');
    $current = current_time('timestamp');
    //Convert difference in seconds to days
    $diffTime = ($current - $posted) / (60*60*24);
    if($diffTime > 365){
        echo '<div class=older-post-message>' . __('This post was written more than a year ago and <em>might</em> not be entirely accurate anymore.', 'wptips') . '</div><br />';
    }
}
add_action('get_template_part_content','tb_older_post_message');