Search
Close this search box.

Add Last Modified Date To Blog Posts

[updated_on]
Share this post:

Whether you like it or not, readers will look at when a post was published and decided whether it is too old to be relevant.

That’s where adding a “last updated” date to your posts becomes important. You see it on tons of websites including The Wall Street Journal, the New York Times and Mom & Pop’s Record Store.

The reason is it helps calm the reader’s fears. They don’t want to waste their reading something that is not relevant or is outdated. Alerting them to the fact that the content is updated encourages them to read further with confidence.

It also gives them confidence that there’s someone actively working on the site.

In the tutorial below I show how to include the code to your functions.php file and have it appear right before the content. The process is automatic, so once you set it up you never have to worry about it again.

Code for the functions.php file

Place this code at the bottom of your functions.php file. The output of this code looks like this:

function wpll_date_updated( $content ) {
      $timeOfPublishing = get_the_time('U'); 
      $timeOfUpdate = get_the_modified_time('U'); 
      //compare "time of publishing" to "time of update" plust 12 hours (43200 seconds) to see if the modified date is more recent
      if ($timeOfUpdate >= $timeOfPublishing + 43200) { 
          $updateDate = get_the_modified_time('F jS, Y');
          $updateTime = get_the_modified_time('h:i a'); 
          $modifiedDateOnPost .= '<span class="modified-on">Updated on '. $updateDate . ' at '. $updateTime .'</span>';  
      } 
   
      $modifiedDateOnPost .= $content;
      return $modifiedDateOnPost;
}
add_filter( 'the_content', 'wpll_date_updated' );

Code for PHP template files

Here’s another piece of code that will allow you to put the last updated date anywhere on your template files. The output of this code (with CSS applied) looks like this:

<?php 
$published_time = get_the_time('U'); 
$modified_time = get_the_modified_time('U'); 
if ($modified_time = $published_time + 259200) { 
   echo "<time class="\&quot;update-date\&quot;">";
   echo the_modified_time('F jS, Y');
   echo "</time>";
} 
?>

What it does is get the published time of the post and compares it to the modified time. Both of which are contained in the database. If the modified time happened within 259200 seconds (or 3 days) of publishing then the “last updated” notification isn’t not put on the post. You can change that time frame to anything you want, just make sure it’s in seconds.

The output of this code is just date in this format, “September 22, 1902”.

I have created the CSS below which adds “Updated ” before the dated

.update-date {
    color:#fff;
}

.update-date::before {
    content: "Updated ";
    background-color: orange;
    border-radius: 5px;
    padding: 3px 7px;
    font-size: 14px;
    margin: 0 7px 0 36px;
}

You can change “Updated ” to anything you like and it will be printed before the date. I’ve also added some CSS styling to make it appear on an orange background.

Only Show Update Date On Posts

WordPress provides us with a boatload of conditional tags that we can use to define one which pages or types of pages code should be executed.

To make “modified on” appear only on posts we need to add another IF statement that checks to see if the page being loaded is a post. We do that using the is_single() conditional tag. The first code example from above will be changed to this:

function wpll_date_updated( $content ) {
      if (is_single()) { 
            $timeOfPublishing = get_the_time('U'); 
            $timeOfUpdate = get_the_modified_time('U'); 
            //compare "time of publishing" to "time of update" plust 12 hours (43200 seconds) to see if the modified date is more recent
            if ($timeOfUpdate &gt;= $timeOfPublishing + 43200) { 
                $updateDate = get_the_modified_time('F jS, Y');
                $updateTime = get_the_modified_time('h:i a'); 
                $modifiedDateOnPost .= '<span class="modified-on">Updated on '. $updateDate . ' at '. $updateTime .'</span>';  
            } 
   
            $modifiedDateOnPost .= $content;
            return $modifiedDateOnPost;
      }
}
add_filter( 'the_content', 'wpll_date_updated' );

The second code example from above will be changed to this.

<?php 
$published_time = get_the_time('U'); 
$modified_time = get_the_modified_time('U'); 
if ($modified_time -->= $published_time + 259200 &amp;&amp; is_single() ) { 
   echo "<time class="\&quot;update-date\&quot;">";
   echo the_modified_time('F jS, Y');
   echo "</time>";
} 
?>

As always, feel free to edit/change any of this code to suit your needs!

Share this post:

2 thoughts on “Add Last Modified Date To Blog Posts”

Leave a Comment

Your email address will not be published. Required fields are marked *

WPLearningLab