In this guide, we are going to guide you that “How to display last updated date of the post in WordPress website”.

Beginners have no idea that why date and time are important for search engine because it wants the updated or new good article.

read and follow all the methods of instruction.

NOTE:- Backup your website before making any changes to your site.

Why display last updated date of the post in WordPress?

There are many WordPress website where daily or weekly old posts are updated.It is very important.

FOR EXAMPLE:- you have updated any of your posts and it is still showing publish date then people will not know that what changes you have made in that article, then users will miss those updates.

So, it is very important to show updated date because it will help your readers to see the changes.

Some website only shows the last updated date.They replace “publish date” with “ last updated date ‘.so in this article we will also show you how you can modify it.

 

Three methods to display  last updated date of the post in WordPress

1.display the last updated date before your post content.

2. Show last post updated date on your theme’s templates.

3.Replace “ publish date “with “last updated date “ by editing theme template.

So here are the instruction of 3 methods

1.Show last updated date before your post content

You will have to paste this code to function.php of your theme’s.


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 .'</p>'; 
}
$custom_content .= $content;
return $custom_content;
}
add_filter( 'the_content', 'wpb_last_updated_date' );

This code will help you to show the last updated date before the post content if publish date and last modified date are different.

it will look like this after pasting the codes

 

 

2. Show last post updated date on your theme’s templates

In this method, you need to edit specific WordPress theme file. Many WordPress themes now using their own template tag to display date and time in post.

Few normal themes use single.php or any other specific template file to show meta data like date and time.

You need to find the codes which are helping to display the date and time and then you need to add this code after the 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 " "; }

screenshot after adding updated code in theme templates

 

 

3. Replace “ publish date “with “last updated date “ by editing theme template.

This method is the best method because it will replace the publish date with last modified date.

It is very good for google search engine and it also helps your website to get a higher rank.

It is very difficult to guide you in this method because you need to find the code in your theme template, which is responsible for showing date and time.

After finding the code, you need to replace that code with this code


<?php $u_time = get_the_time('U');
$u_modified_time = get_the_modified_time('U');
if ($u_modified_time >= $u_time + 86400) {
echo "Last updated on ";
the_modified_time('F jS, Y');
echo "  "; }
else {echo "Posted on "; the_time('F jS, Y');} ?>

Don’t forget to save the changes.

It will look like this in your posts after pasting the code into a specific file of your theme.

display only last updated date of the posts

 

We used this method but we don’t display the “posted on” or “ updated on “.we only display the date of published and date of the last update.

 

you can also check this guide:-

Categories: wordpress

Leave a Reply

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