BACK TO TOP

How to Add the “Last Updated” Date in WordPress

How to Add the “Last Updated” Date in WordPress

If you’re a blogger, an online news publisher, or if you own any other type of article-based website, it’s important to let your visitors know that the articles you post are fresh and up to date. Even though WordPress doesn’t come with this feature right out of the box, there is still an easy way to add the “last updated” date to your WordPress posts.

Today, we’re going to share some simple steps that will allow you to display the date when your article was last updated and make things more convenient for your website visitors:

When to Consider Adding Last Updated Date to Your WordPress Posts

It’s common for most WordPress themes to display the date of publishing for each article, which is also what works for most websites. That being said, some websites post content that gets updated regularly, for one reason or another.

For example, in the case of a news website, there’s a high chance an old story will need to be updated at some point. This can be an update that contains the latest information about any recent happenings related to the topic at hand that have occurred in the meantime, i.e. after the article has been posted. Also, various online tutorials, product and service reviews, tour guide articles, etc. can all contain time-sensitive information. The relevance and freshness of these types of content can depend on different factors, such as the pricing, changes in product features, terms of use, and so on. As such, they should be updated regularly. And if you want your users to be aware of those updates, showing the last updated date on your posts can be a good practice.

Also, for many users, articles that are more than two years old will usually be dismissed as no longer relevant, even if they somehow appear at the top of Google search results.

The fact of the matter is, the older your post is, the less are the chances it’ll get clicked on. Google takes this factor into account when it comes to its ranking algorithm as well, and also displays the date a post was published (or updated) in SERPs.

Publishing Date

That being said, there are articles written years ago that have the evergreen type of content still relevant to this day. Still, many websites make sure to update these types of articles regularly so as not to waste the potential these articles have. This way, they’re indicating to the users that the content they’re interested in is still fresh. If you’re also among those who want to prevent their high-quality posts from losing much-needed views (and quite frankly, why wouldn’t you be?) we advise you to do the same.

If you have content that you intend updating but still want to make sure your readers know it’s not exactly fresh, there’s a neat workaround you can use. Displaying an “Old Post” notification above your post will let your visitors know they need to take the content with a grain of salt. It’s easy to do with the help of a handy little plugin, and it will get you a long way in terms of UX and trust. It shows that you’re regularly revisiting your content making sure everything is in order, and it also helps with accountability in case your post contains info that is no longer valid.

But that’s a subject we tackled in a separate article, linked above, so right now let’s get back to our topic here, which is adding the last updated date to your posts.

While WordPress does not come with the option to display the last modified date of your post by default, luckily, there’s always an option to do it manually. All you need to do is insert a few lines of code in the files of the WordPress theme you’re using – and this time around, we’re going to show you how.

Without further ado, here are some easy instructions you can follow to display the “Last Updated” date in WordPress. You can either achieve this by editing the theme’s functions.php file or inserting a code snippet into one of your theme’s template files.

Qode Themes: Top Picks
Bridge New Banner
Bridge

Creative Multi-Purpose WordPress Theme

Stockholm WordPress Theme
Stockholm

A Genuinely Multi-Concept Theme

Startit WordPress Theme
Startit

Fresh Startup Business Theme

Displaying the Last Updated Date Using Functions.php

To add the last updated date before your content, you need to add a line of code into your theme’s functions.php file.

Go to Appearance >> Theme Editor in your admin dashboard and find your functions.php file in the Theme Files list.

Theme Functions

Then, insert the following code at the very end of the file:

function my_last_updated_date( $content ) {
$u_time = get_the_time('U');
$u_modified_time = get_the_modified_time('U');
if ($u_modified_time >= $u_time + 86400) {
$updated_date = get_the_modified_time('F jS, Y');
$updated_time = get_the_modified_time('h:i a');
$custom_content .= '<p class="last-updated entry-meta">Last updated on '. $updated_date . ' at '. $updated_time .'</p>';
}
$custom_content .= $content;
return $custom_content;
}
add_filter( 'the_content', 'my_last_updated_date' );

The code works by checking whether there’s a difference between a published date of the post in question and its last modified date. If these two dates are different, then the last modified date will be displayed.

Theme Functions Code

Once you insert your code, make sure to hit the Update File button.

If you want, you can even add some custom CSS to your theme’s stylesheet (or by going to Appearance >> Customize and using the Additional CSS option). For example, you can use this code as your base:

.last-updated {
font-size: small;
text-transform: uppercase;
background-color: #fffdd4;
}

Here’s what our final result looks like:

Last Updated Date

Displaying the Last Updated Date Using Theme Templates

Another way to do this is to use the specific template file of your theme. Most themes use different templates for the purpose of displaying posts, like single.php, index.php, archive.php, and so on.

What you need to do is find the code within the file that displays your date and time, and then either replace the already-existing code with the one we will share with you below, or insert it right underneath the theme’s “date and time” code. The former will only display the last modified date of your post (without the original published date), while the latter will allow you to have both dates displayed above your content.

We used the Twenty Seventeen theme for this purpose. The date and time code for this theme is located in the template-tags.php file.

What we did was find the function called ‘twentyseventeen_time_link’ and placed the following right below the ‘get_the_time’ line of code:

$u_time = get_the_time('U'); 
$u_modified_time = get_the_modified_time('U'); 
if ($u_modified_time >= $u_time + 86400) { 
echo "<p class="last-updated entry-meta">Last modified on "; 
the_modified_time('F jS, Y'); 
echo " at "; 
the_modified_time(); 
echo "</p> "; }

You can use the same code within your own theme’s template file.

Displaying the Last Updated Date Using Theme Templates

Just hit the Update File button when you’re done, and that’s it – your post will now display the date when your post was last modified.

And here’s what it looks like on our end:

Last Modified Date

Wrapping Up

If you’re a website or blog owner that strives to update their content regularly, then you might want to consider adding the “Last Updated” feature to your WordPress posts. This practice is particularly convenient as it allows you to show your visitors that your content is always fresh and relevant. As you can see from our tutorial, the method that allows you to insert the code directly into your theme’s functions.php is relatively simple even for beginners and does the job quite well. Alternatively, you can also easily display your post’s last modified date by adding the code into one of your theme’s template files. Finally, don’t forget that you can also display a notification that says your post is relatively old, indicating you’re planning to update it as soon as possible.

By employing these easy techniques, you can help boost the trust your readers have in you and your blog or website and establish it as a reliable and accurate source of content.

Post your comment

Comments2
  • Ekta
    3 years ago

    Hi, the code is working and it is showing on both pages as well as posts. I want to remove it from pages, can you please tell me an easy way to do that?

    Thanks,
    Ekta

    • Qode Interactive
      3 years ago

      Hi Etka, thanks for writing in 🙂
      If you used the first code snippet from the article (the one for the functions.php file), then all you need to do is add the following CSS to your site:

      body.page .last-updated.entry-meta {
      display: none;
      }

      If, on the other hand, you used the second solution (via theme templates), we suggest you copy the code from the article again (because we recently updated it), and then you’ll be able to use the same CSS from above to remove the date from pages.

      Best Regards,
      Qode