If you’re using a professionally designed WordPress theme, your posts probably show your visitors the date the article was published. This is standard and is more than enough for some websites, but when your articles are regularly updated, it’s great to allow your visitors to see when they were updated. That way if they’re accessing information that rapidly changes, they’ll know how recent the information is.
News sites are the most common type of website whose visitors benefit from posting the date each article was updated. Each time a story gets published, there are always more pieces of the puzzle that come in, and it helps visitors to know the information has been updated.
No matter what kind of website you’re running, if you want to add the “last updated” date to your published WordPress articles, there are three ways you can accomplish this. The first is through adding some code to your “functions.php” file, the second is to directly modify your theme’s files, and the third is by using a plugin. Each of these options have different benefits, so here’s an outline of how to accomplish each one.
1. Adding custom code to your functions.php file
The best and most permanent way to show the latest updated date on your articles is to add the following code directly to your “functions.php” file. (It is located in your theme folder.)
Note: Always back up your functions.php file. Sometimes WordPress updates core files, and it’s possible that your functions.php file could end up being replaced during an update or during a manual update of the core files, so back everything up any time you make changes.
This code works by checking the post publication date and comparing it against its “last modified” date to determine if the two dates are different. If they are different, the post will display the “last modified” date with the text “Last updated on.”
Altering the style of the “last updated” date display
If you want to customize the style of the “last updated” text, you can create a new style in your CSS file for the class
.last-updated since that is the class defined in the code above. You can name the class anything you’d like, or you can assign the paragraph tag an existing class to match your site’s theme. You can also change the paragraph text from “Last updated on” to anything you’d like it to read, like “This post was last modified on,” for example.
Depending on your level of development, you might be aware that the functions.php file isn’t always the best choice to use for certain customizations. Rest assured that using “functions.php” to add the “last updated” date to your articles is a completely legitimate move.
2. Modify your theme’s files directly
The next option is to modify your theme’s files directly. These files would be “index.php,” “single.php,” and “page.php” and could include other files that display the date on individual posts, depending on how complex your theme is.
You’ll want to find the following code, which could be written in one of two ways.
Code example 1:
Code example 2:
Once you’ve found the code as written in one of the lines above, replace every instance of it with the following code:
One caution about modifying theme files
Whenever you directly modify one of your theme’s files in WordPress, you risk losing your customizations when you update your theme. Updating a theme will overwrite the theme’s CSS files as well as other files, and the only way to avoid this problem is by installing and modifying a child theme instead.
If you’re unfamiliar with child themes, you should learn more about them, especially if you plan on customizing your WordPress theme. Child themes are not as complex as they sound, and they’re not hard to create. A child theme isn’t just a second version of your main theme like some people might think. It’s a set of just a few core files, including the functions.php and CSS files that tell WordPress to use the main theme and also use the code from the files inside the child theme as the dominant code. In other words, if your theme’s CSS file has the background color set to black, but your child theme’s CSS file has the background color set to blue, the child theme’s CSS style will dominate and override the main theme’s CSS.
This ensures that no matter how many times you update your theme, your customizations will remain intact.
3. Use a plugin
Using a plugin like this one is the easiest way to show the “last updated” date on your WordPress posts because you don’t have to look for or replace any code. Just be sure to keep the plugin updated to reduce security risks.
As with any plugin, the downside is that some plugins can conflict with theme functions, so if you install a new theme and find that your last modified timestamp plugin is the culprit, you’ll want to try one of the other two methods described above.
Just make sure to back everything up before you modify anything. Download a copy of your PHP files before you make any changes, just in case you make a mistake. Remember that PHP is sensitive even to extra spaces, so backing up your files before making even the smallest change is the smart thing to do.