How to Display Updated Date of Your Post in WordPress

There are times when you write something on your Posts and after a certain period of time thing changes. When people visits your site and reads that post, they might get totally confused just because it was written before the updates or the development took place. Especially, if you are writing on news type of posts, I think it’s very important for your readers to know that the post has also been updated with the recent developments.

There are ways to show the Updated date of your post which is very easy to implement on your WordPress based blog site. We will give you the code shortly but before that I must warn you that “Do it at your Own Risk“, we won’t be liable for any sort of damage or data loss that might occur from our snippet codes that we provide.

Now, just to display the updated dated, you can simply copy and paste the following line on your single.php, page.php, index.php etc. file after the default post date code which is something like this (<?php the_time(‘F jS, Y’) ?>).

<?php the_modified_time(‘F jS, Y’);?>

WordPress TipsDefinitely you can change the format of the date. You can find some really helpful information on this from the WordPress Codex. Now, part of the problem with this code is, it will only show the post Updated date and time. It might so happen that there are posts that you never updated. In that case will show the post’s published date but what if you want to show the Original published date and also the updated date (if there is any)?

Using the above code with the default WordPress code (<?php the_time(‘F jS, Y’) ?>) is bit problematic. It will repeat the same date (when it was published). This is where the tricky part comes in. It’s simple to implement though. Just copy and paste the following code after the default code that we mentioned earlier.

<?php $u_time = get_the_time(‘U’);
$u_modified_time = get_the_modified_time(‘U’);
if ($u_modified_time >= $u_time + 86400) {
echo “, Updated: “;
the_modified_time(‘M j, Y’); } ?>

This is what exactly we are using on our site as well and working fine. This code will show the updated date if and only if you ever update any post or else it will show nothing. To see the sample of this code, simply take a look at this post that we wrote before and updated it later on. I hope this will help you to get the clear cut idea.

Like always, feel free to ask questions or make comments. If you find this post to be helpful show your support either by liking our Facebook fan page or adding us to your Google+ Circle.

You can follow us on Twitter, add on your Google+ circle or Like our Facebook page to get the latest news, updates and reviews.

advertisement
Comments: