It is better to show the last updated date in the articles in addition to the created date. This will always help the readers to ensure the freshness of the post.
It can easily be done on a WordPress site. There are two methods using which the last updated date can be displayed. The first way is to directly update the theme’s post (usually single.php) file and include the last modified date.
Another way is to use the additional filter in functions.php and update the post content and enter the last update date. Let us now look at both methods one by one in this article.
Is it important to display the last updated post? Before thinking about the last updated post, it is really important to show the created date of the article. I would say both are good from your user’s point of view. It will allow end users to evaluate the freshness of the post.
I used to update my old articles to keep them updated with the latest technology versions, usage, and trends. It needs to reflect the users and is the best way to show the last updated date of the post.
Most of the popular blogs and websites don’t display the last modified date and worse, they don’t even display the created date. This needs to change.
Method 1: Modify the theme template file for the 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.
$local_timestamp = get_the_time('U');
$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 a conditional basis. If you wish, you can omit the time part and display only the date.
Method 2: Add a filter and update the content to show the 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 codes have the same logic, but the way WordPress links to the theme file is different. Hope you have now learned how to display the last modified date and time of a post in WordPress.
Got any questions? Any addition? Feel free to leave a comment.
Thank you for reading