How to Display the Last Updated Date of Your Posts in WordPress

How to Display the last updated dated of your posts in WordPress

Do you want to display the last updated date of your posts in WordPress? Well, this will require you to work with code, but don’t worry! It’s very easy. Some websites regularly update their posts and like to show their users when the article was last updated. ­

Most WordPress themes usually show the date when a post was published. However, WordPress also gives you the power to display the last updated date of your posts. This feature is crucial for websites where old articles need to be updated regularly.

The most common example here can be news websites. They often have to update the old stories to show new developments, add corrections, or media files to let their users know about the latest updates on certain news. If only added the published date, their users would miss those updates.

Now, let’s see how you can easily display the last updated date of your posts in WordPress.

Display the Last Updated Date of Your Posts in WordPress

As said earlier, this will require you to add code to your WordPress files. You can display the last updated date of your posts in WordPress in two ways. One is showing last updated date before Post Content and another is adding last updated date in Theme Templates. Now, let’s check out these methods in detail.

Method 1: Display Last Updated Date before Post Content

You will have to add the following code to your theme’s funtions.php file or a site specific plugin in this method.

function wpb_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">Last updated on '. $updated_date . ' at '. $updated_time .'</p>;
} 

 $custom_content .= $content;
 return $custom_content;
}
add_filter( 'the_content', 'wpb_last_updated_date' );

The code will check if a post’s published date and last modified dates are different. And if they are, then it will display the last modified date before the post content.

You can also add custom CSS to style the appearance of the last updated date. Below is a little CSS you can use to start off:

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

After that, this is how the updated date of your posts will look like:

last updated date of your posts in WordPress. Image Credit: WP Beginner

Method 2: Add Last Updated Date in Theme Templates

In this method, you will have to edit specific WordPress theme files. Many WordPress themes now use their own template tags which define how these themes show post meta data like date and time. Some themes also use content templates or template parts to display posts.

Few simpler themes will use single.php, archive.php, and other template files to show content and meta information.

You need to look for the code responsible for displaying the date and time. Then, you can either replace that code with the following code, or add it right after your theme’s date and time code.

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

This will display your latest updated date in the following manner:

Last modified date of your posts in WordPress. Image Credit: WP Beginner

That’s all there is. All you need to do is add code to your website and you will be able to display the last updated date of your posts in WordPress easily.

Have anything to add, share or ask? Drop your comments below.

 

Source: WP Beginner

Leave a Reply

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