Skip to content
  • Homepage
  • HTML
  • CSS
  • Symfony
  • PHP
  • How to
  • Contact
  • Donate

Teach Developer

Articles, Guides & Tips

How to display the last updated date of a post in WordPress

Home  »  Wordpress   »   How to display the last updated date of a post in WordPress
Posted on August 2, 2022August 2, 2022 No Comments on How to display the last updated date of a post in WordPress
644

It is better to show the last updated date in the articles in addition to the created date. This will always help the readers to ensure the freshness of the post.

It can easily be done on a WordPress site. There are two methods using which the last updated date can be displayed. The first way is to directly update the theme’s post (usually single.php) file and include the last modified date.

Another way is to use the additional filter in functions.php and update the post content and enter the last update date. Let us now look at both methods one by one in this article.

Is it important to display the last updated post? Before thinking about the last updated post, it is really important to show the created date of the article. I would say both are good from your user’s point of view. It will allow end users to evaluate the freshness of the post.

I used to update my old articles to keep them updated with the latest technology versions, usage, and trends. It needs to reflect the users and is the best way to show the last updated date of the post.

Most of the popular blogs and websites don’t display the last modified date and worse, they don’t even display the created date. This needs to change.

Method 1: Modify the theme template file for the post

The simplest way is to edit the WordPress theme post file and generally, it will be single.php in your theme folder. We will just get the current post’s modified time using the WordPress function get_the_modified_time(‘U’) and format it to display to the user.

$local_timestamp = get_the_time('U'); 

$lastModifiedTime = get_the_modified_time('U'); 

if ($lastModifiedTime >= $local_timestamp + 86400) { 
    echo "<p>Last modified on "; 
    the_modified_time('F jS, Y'); 
    echo " at "; 
    the_modified_time(); 
    echo "</p> "; 
} 

In the above code, we are displaying the last modified date and time only on a conditional basis. If you wish, you can omit the time part and display only the date.

Method 2: Add a filter and update the content to show the last modified date

Hope you are aware of the functions.php file of the WordPress from your theme folder. We can add a function and register it as a filter for ‘the_content’. This will get the post content and prefix it with the last modified date and time information.

function lastModifiedAt( $postContent ) {
    //get the local time of the current post in seconds
    $local_timestamp = get_the_time('U'); 

    //get the time when the post was last modified
    $lastModifiedTime = get_the_modified_time('U'); 

    if ($lastModifiedTime >= $local_timestamp + 86400) { 
        $modifiedDate = get_the_modified_time('F jS, Y');
        $modifiedTime = get_the_modified_time('h:i a'); 
        $updatedInfo = '<p class="last-updated">Last modified on '. $modifiedDate . ' at '. $modifiedTime .'</p>';  
    } 
 
    $updatedPostContent = $updatedInfo . $postContent;
    return $updatedPostContent;
}
add_filter( 'the_content', 'lastModifiedAt' );

Both codes have the same logic, but the way WordPress links to the theme file is different. Hope you have now learned how to display the last modified date and time of a post in WordPress.

Got any questions? Any addition? Feel free to leave a comment.

Thank you for reading

Wordpress Tags:PHP

Post navigation

Previous Post: How to add two-factor authentication (2FA) to WordPress using the Google Authenticator plugin.
Next Post: How to Disable WordPress Auto Update? Turn Off WordPress Auto Updates

Related Posts

  • WordPress Installation Steps
  • How to Install a WordPress Plugin for Beginners
  • How to Disable WordPress Auto Update? Turn Off WordPress Auto Updates
  • How to add two-factor authentication (2FA) to WordPress using the Google Authenticator plugin.
  • How to Delete Unused Database Tables in WordPress
  • How to Display Breadcrumb without Plugin in WordPress

Leave a Reply Cancel reply

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

Categories

  • Codeigniter (3)
  • CSS (11)
  • eCommerce (1)
  • Framework (1)
  • Git (3)
  • How to (43)
  • HTML (5)
  • JavaScript (15)
  • Jquery (7)
  • Laravel (1)
  • Linux (4)
  • Magento-2 (1)
  • Node js (4)
  • Others (2)
  • PHP (11)
  • React (13)
  • Server (1)
  • SSH (3)
  • Symfony (6)
  • Tips (16)
  • Top Tutorials (10)
  • Ubuntu (3)
  • Vue (1)
  • Wordpress (7)

Latest Posts

  • What is SSH in Linux?
  • How to Delete Files in Ubuntu Command Line
  • How to Deploy a React application on a cPanel
  • How to use events listeners and Event Subscriber in Symfony
  • How to Convert PHP CSV to JSON

WEEKLY TAGS

AJAX (1) Codeigniter (1) Javascript (11) JQuery (1) PHP (16) Programming (1) React (3) Symfony (1)

Random Post

WordPress Installation Steps
How to Symfony Request
Enable or Disable SSH Root Login Access in Linux
How to create the first Git Repository
What is SSH in Linux?

Quick Navigation

  • About
  • Contact
  • Privacy Policy

© Teach Developer 2021. All rights reserved