Do you want to show the last updated date on blog post in WordPress. Some websites regularly or weekly update their blog posts and they want to let the user know when was it updated. If you are looking for such thing you have landed on right spot.
When To Show Last Updated Date on Blog Post in WordPress
Many WordPress themes only show the date when the post was first time published. It is fine for some websites but not for everyone like media websites.
Sometimes you need to notify the user about the content update.
The best way is to show the modified date. It is trend in modern days, the updated content is more loved by the people than non-updated.
In WordPress, many things change and will change. If you are a regular WordPress blogger, you should update the content when needed.
Showing a modification date to user is best way to motivate him about the content he/she is reading.
Let’s begin the work.
2 Ways To Show the Last Updated Date On Blog Post In WordPress
You can display the modification date in many ways. But here, we will do it in two ways.
- Manually displaying post Modification date.
- Using plugin to show modified date.
Manually Displaying Post Modification Date
We have lots of ways to do thing we want. One such is just echoing the get_the_modified_date()
.
But I wouldn’t recommend this way. Because we don’t update posts regularly unless needed.
So if you wrap this function inside HTML, you may see unwanted design change like empty space, change in padding or anything else.
We need to use conditionals to make it work properly e.g; if the post has been modified, then show the modified date else not.
You can do so by using this code snippet.
$date = get_the_date('U'); $modified_date = get_the_modified_date('U'); if ($modified_date >= $date + 86400) { echo "Last Updated On: " . get_the_modified_date(); } else { echo "Not Updated Yet"; }
Here, I am creating two variables, $date
and $modified_date
. Inside them I am storing functions which do the whole work.
First variable $date
, holds get_the_date()
function with argument 'U'
which returns article date in Unix timestamp like this 1455880176
.
The reason for using Unix-stamp is that we have to calculate between the two dates,
Second variable $modified_date
, holds the information of modified date in Unix-stamp.
After setting these variables, we are using conditional to check whether the post has been updated or not. If the post has been updated, the modified date will shown otherwise it will echo else statement.
Another way is to use different functions. The following code does the same work but the functions are not same.
$time = get_the_time('U'); $modified_time = get_the_modified_time('U'); if ($modified_time >= $time + 86400) { $modified_date = get_the_modified_time('M j, Y'); $modified_time = get_the_modified_time('h:i a'); $custom_content = '<p class="last-updated">Last updated on '. $modified_date . ' at '. $modified_time .'</p>'; echo $custom_content; } else { echo "Not Updated Yet"; }
You can check, I am using get_the_time()
instead of get_the_date()
. Remember you, this code will help you in showing time also.
Note: You can use this code block by adding them in core files. But if you are beginner, you should consider applying the below code to functions.php
to get the same effect.
function wg_modified_post_date( $output ) { $time = get_the_time('U'); $modified_time = get_the_modified_time('U'); if ($modified_time >= $time + 86400) { $modified_date = get_the_modified_time('M j, Y'); $modified_time = get_the_modified_time('h:i a'); $custom_content .= '<p class="last-updated">Last updated on '. $modified_date . ' at '. $modified_time .'</p>'; } $custom_content .= $output; return $custom_content; } add_filter( 'the_content', 'wg_modified_post_date' );
The best thing you should consider is to use functions. You can wrap the code block inside function and use it anywhere you want.
The benefit of using function is that, if someday the code generates the error, you will be able to fix that in one place. So it is a nice way. The function will look like.
if (!function_exists('check_modify')) { function check_modify() { $date = get_the_date('U'); $modified_date = get_the_modified_date('U'); if ($modified_date >= $date + 86400) { echo "Last Updated On: " . get_the_modified_date(); } else { echo "Not Updated Yet"; } } }
You may have seen that I am using an additional conditional statement before the function. The only purpose of it is, it will check whether any other function with same name exists or not. If the function name is found, the function wouldn’t be called.
Until now I defined what the different codes does. But if you want to apply theme in live site, how will you do that.
Well the answer is much straightforward. You have to edit the theme files like single.php, archive.php, etc. Let’s do this.
First add the whole function check_modify()
into functions.php
. Now we can call the function anywhere.
Next navigate to single.php
file and call the function where you want it to show but inside PHP tags. You should get the result like this.
Using plugin to show modified date
If you don’t want to play with code, you can install a plugin named as “Post Modified Date“.
On successful activation, it will start working in background. One thing I dislike about the plugin is that, if you didn’t updated the post it will show the article date instead.
To overcome this problem I thought to write a plugin of my own named as “Easy Post Modified Date”.
It will show the modified date and if the post hasn’t been updated it will show “Not Updated Yet”. Another feature I added to it is short-code. You can show the post updated date anywhere.
I hope you loved these 2 ways to show the last updated date on blog post in WordPress. If so, please do share with your friends and leave a comment.
Leave a Reply