How to add the last updated date to posts’ bylines in WordPress

One of the things that I was reminded of as I set up this site is just how much of WordPress’s behavior is controlled by the specific WordPress theme that you use. This includes the date that is displayed in the byline of each post. For instance, by default the Mesmerize theme only displays the original published date, and not the updated date (a.k.a. last modified date). Fortunately, there is a pretty simple fix for this, even for those who—like me—are not well-versed in PHP.

Here is how I was able to add the updated date to my posts in the Mesmerize theme. This should be a fairly similar process for other WordPress themes as well.

You’ll have to access your theme’s files, which can be done a number of ways. For example: WordPress’s Appearance -> Theme Editor menu, the file manager in your hosting provider’s cPanel, or an FTP/SFTP connection through something like FileZilla.

Part 1: Adding the last updated date to post bylines

In Mesmerize, posts are wrapped in a div with the class post-content-single, so the relevant file for me to edit was \wp-content\themes\mesmerize\template-parts\content-post-single-header.php.

Before editing this file, the byline looks like this:

mesmerize post byline with published date only

This is the code that is populating the part with the published date after the calendar icon:

<span class="post-date"><?php echo "Published: "; the_time(get_option('date_format')); ?></span>

To get this byline to also include an updated date (if there is one that is different from the original published date), we can modify this line of code to include a conditional statement that checks to see if the last modified time is at least 86,400 seconds (i.e. one day) apart from the published date:

<span class="post-date"><?php echo "Published: "; the_time(get_option('date_format')); if(get_the_modified_time('U') >= get_the_time('U') + 86400) {echo " | Updated: "; the_modified_time(get_option('date_format')); } ?></span>

After pushing this updated PHP file, the byline for the previous example post looks like this:

post byline with updated date appended

Part 2: Don’t forget about post previews on category pages

I got what I wanted, but it’s not quite mission accomplished yet. There’s still one more thing we need to do. Even after that edit for post bylines, Mesmerize’s default behavior for category pages is to display the original published date rather than the last modified date in each post’s preview:

category post preview displaying published date

This is controlled by the following line of code within the \wp-content\themes\mesmerize\template-parts\content-list-post-meta.php file:

<span class="date"><?php echo the_time( get_option( 'date_format' ) ); ?></span>

This time, I only want to display one date, even if the published and updated date are different from each other (pretty arbitrary, I know), so we get to replace the line above with an if else statement:

<span class="date"><?php if(get_the_modified_time('U') >= get_the_time('U') + 86400) {echo the_modified_time(get_option('date_format')); } else {echo the_time(get_option('date_format')); } ?></span>

Which results in the published date being replaced by the updated date for the post preview snippets on category pages:

category post preview displaying updated date


This solution does have some limitations. The most common one is that it will not solve for some edge cases of next-day updates due to the if statement checking for a difference of 24 hours between the published and updated date. You could make an update within 24 hours but on a different day; for example, if I publish a post at 10 pm tonight and edit it 4 hours later at 2 am tomorrow, those are two different dates. However, the byline would still display only the published date because the span of merely 4 hours would not trip the 86,400 seconds condition from our if statement. There is a way around this with some slightly more elegant code, but I didn’t bother pursuing it (though it might be a quick mini-update at some point if I feel like it). At the end of the day, this solution works for my purposes.

Is this a bad solution? Have I exposed how much of a noob I am at PHP? Feel free to let me know in the comments.