How to Display the Last Updated Date of Post in WordPress

Last modified on June 5th, 2018 by Vincy.

It is better to display the last updated date in the articles in addition to the created date. This will always help the readers to ascertain the freshness of the post. It can be done easily in a WordPress site. There are two ways using which the last updated date can be displayed. The first way is to directly update the theme’s post (generally single.php) file and include the last modified date. The second way is to use add filter in functions.php and update the post content and insert the last updated date. Let us see both the methods one by one now in this article.

How-to-Display-the-Last-Updated-Date-of-Post-in-WordPress

Is it important to display the last updated post? Before thinking about the last updated post, it is really important to show created date of the article. I would say that both are good from your user perspective. It will allow the end users to gauge the freshness of the post. 

I used to update my old articles to keep it updated with latest tech versions, usage and trend. It needs to be reflected to the users and the best way is to display the last updated date of the post. Most of the popular blogs and website do not display the last modified date and to be worse, they do not even display the created date. This needs to change.

Method 1: Modify the theme template file for post

The simplest way is to edit the WordPress theme post file and generally it will be single.php in your theme folder. We will just get the current post’s modified time using the WordPress function get_the_modified_time(‘U’) and format it to display to the user.

//get the local time of the current post in seconds
$local_timestamp = get_the_time('U'); 

//get the time when the post was last modified
$lastModifiedTime = get_the_modified_time('U'); 

if ($lastModifiedTime >= $local_timestamp + 86400) { 
    echo "<p>Last modified on "; 
    the_modified_time('F jS, Y'); 
    echo " at "; 
    the_modified_time(); 
    echo "</p> "; 
} 

In the above code, we are displaying the last modified date and time only on conditional basis. If you wish, you can omit the time part and display only the date.

Method 2: Add filter and update the content to show last modified date

Hope you are aware of the functions.php file of the WordPress from your theme folder. We can add a function and register it as a filter for ‘the_content’. This will get the post content and prefix it with the last modified date and time information.

function lastModifiedAt( $postContent ) {
    //get the local time of the current post in seconds
    $local_timestamp = get_the_time('U'); 

    //get the time when the post was last modified
    $lastModifiedTime = get_the_modified_time('U'); 

    if ($lastModifiedTime >= $local_timestamp + 86400) { 
        $modifiedDate = get_the_modified_time('F jS, Y');
        $modifiedTime = get_the_modified_time('h:i a'); 
        $updatedInfo = '<p class="last-updated">Last modified on '. $modifiedDate . ' at '. $modifiedTime .'</p>';  
    } 
 
    $updatedPostContent = $updatedInfo . $postContent;
    return $updatedPostContent;
}
add_filter( 'the_content', 'lastModifiedAt' );

Both the code has the same logic, but the way that is hooked into the WordPress theme file is different. Hope you have now learnt to display the last modified date and time of the posts in WordPress.

 

This WordPress code tutorial was published on June 5, 2018.

↑ Back to Top

Share this Article