Archived topic
Show last update date on posts and google results
23 replies · Started by Fekry on April 26, 2020
Word "updated" still shows in every POST(updated or just published).
I"ve published a fake post with name "Helli David" and word "updated"shows in it.
I’ve put a new URL link in the orginal message for the fake post I just published.
I need word “updated” to show only in the really updated posts.
What should I do?
Hi Generatepress team
I've spent many hours with you to know how to (show last update date on posts and google results).
But word "updated" till shows in every post (updated or just published).
My site is not ready yet, but I had to disable the coming soon page repeatedly.
Although, I did not receive the support I wish!
Can you make sure that this is the only PHP snippet you have added to your site. Please remove any old snippets and add this code:
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>';
$updated_time = get_the_modified_time( 'U' );
$published_time = get_the_time( 'U' ) + 86400;
if ( $updated_time > $published_time ) {
$time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">%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 );
Yes, it's the only PHP snippet.
And I've put this CSS AS Tom recommend:
.updated-date:before {
content: "Updated on ";
}
.published:before {
content: "Published on ";
}
Now, I've modified the post with Title (Hello David) but the word still "published" not "updated"
OK so that means the code is working :)
It will only show the Updated date IF the update is made more than 24hrs after it was originally published.
This line of code gets our published date and adds 24hrs ( 86,400 seconds ).
$published_time = get_the_time( 'U' ) + 86400;
If you wanted you could change it to 1hr ( 3,600 seconds ) like so:
$published_time = get_the_time( 'U' ) + 3600;
Thanks David. It works now.
Another thing please, I don't want to show word "published" when a post is not updated.
How should I do that?
In that case, you can just remove this part of the CSS:
.published:before {
content: "Published on ";
}
Now it works as I wish.. thanks David, Tom and Leo.
Glad we could help! :)