Archived topic
Post publish and update dates
5 replies · Started by bheemnr on July 5, 2022
Hi,
I have the following code in snippets.
add_filter( 'generate_post_date_output', function( $output, $time_string ) {
$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$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 );
I want to show only the published date for those posts that are published recently but not updated.
And for those updated, I wish to show the first published and last updated date.
Any customization for the above code to achieve that.
Thanks
ranga
Hi Ranga,
Here’s a modification of that PHP snippet you can try:
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 published" datetime="%1$s" itemprop="datePublished">Published on %2$s - </time>
<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">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 );
Hope this helps!
Hi, your code works. Thanks.
But will this code also instruct the search engines about the updates?
Our previous code was aimed to indicate search engines too.
Regards
Hi there,
Fernandos code performs in the same way your previous code did, which is to display the relevant time tag on the page.
Thats all the Theme does.
If you're using an SEO plugin such as Rank Math or Yoast they will generally output date meta in the <head> of your site like so:
<meta property="article:published_time" content="2022-05-20T12:26:28-05:00">
<meta property="article:modified_time" content="2022-06-10T22:04:54-05:00">
Google will crawl both meta data and the dates displayed on screen and it will decide what date to display. The Theme cannot dictate to Google what date it must use.
Hi, thanks for the clarification.
Is there any way to show the month in short form in the above code, like January as Jan and December as Dec with the above code?
Regards
You can control your Date Format in Settings > General --> Date Format, you can use a custom format string eg. M j, Y
See here for more info:
https://wordpress.org/support/article/formatting-date-and-time/