Even if you update your content regularly; you must have been annoyed to see the same old published date on the frontend of your website.
Do not worry, today in this post we will be learning how to display the last updated date on the posts if you are using the GeneratePress theme.
GeneratePress theme by default shows the first published date on the blog posts. So, in this post, we will learn a smart way to change that to show the last updated date on your blog posts.
Also Read: How to show the last updated date on the Newspaper 11 theme
Table of Contents
Why show the last updated date?
Readers love fresh content
Your readers will tend to love the updated and modified fresh content instead of old ones. So, if you don’t have the last updated date on your posts your readers may turn back from your posts. So, as a blogger or a brand, you may want to show a modified date.
Rich Results
When somebody does a search on Google or other search engines, if you have added the last updated date, it will show an updated date else it will show the first published date.
This way users will be more likely to click on the fresh post instead of the stale one. So, you got one more reason to add the last updated date functionality to your GeneratePress theme.
Also Read: How to add cookie consent in WordPress without using a plugin
How to add the last updated date on the GeneratePress theme
To add the last updated date on the GeneratePress theme first we need to install a plugin called WPCode (Previously Insert headers and Footers)
Go to Plugins >> Add New and in the search bar type “WPCode”
Click on Install and then Activate the WPCode plugin
Now from the left side of the dashboard click on Code Snippets >> Add Snippet >> Add your custom code
Add the title to the snippet. It can be anything. For ease, we write “Last updated date”
From the “Code Type” option select PHP
Copy and paste the below PHP code and click on Save; then you are done. You have successfully added the last updated date functionality to your GeneratePress theme.
add_filter( 'generate_post_date_output', function( $output, $time_string ) {
$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">Published on: %2$s</time>';
if ( get_the_date() !== get_the_modified_date() ) {
$time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">Last Updated on: %4$s</time>';
}
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date() )
);
return sprintf( '<span class="posted-on">%s</span> ',
$time_string
);
}, 10, 2 );
The above code compares the date of your blog posts and shows the Last Updated date as ‘Last updated on:‘only if there is a revision to the post, and if the post has not been revised it shows the Published date as ‘Published on:‘.
You can check the before and after of the blog post after adding the last updated code snippet to your GeneratePress WordPress website.
Wrapping Up
In this tutorial, we learnt how to add the last updated date on the GeneratePress theme by using WPCode plugin in the easiest way possible.
If you have anything to ask please feel free to comment down below.
More from GeneratePress.