Archived topic
LAST UPDATE DATE ON POSTS AND GOOGLE RESULTS
25 replies · Started by Alan Gautham on November 16, 2018
I know this topic has been discussed already a few times before. I got the solution with some issues. This is the snippet presently I am using:
if ( ! function_exists( 'generate_posted_on' ) ) :
/**
* Prints HTML with meta information for the current post-date/time and author.
*/
function generate_posted_on()
{
$date = apply_filters( 'generate_post_date', true );
$author = apply_filters( 'generate_post_author', true );
//if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) )
$time_string .= '<time class="updated" 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() )
);
// If our date is enabled, show it
if ( $date ) :
printf( '<span style="float: left; margin-right: 3px;">Updated:</span><span class="posted-on">%1$s</span>',
sprintf( '<a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>',
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
$time_string
)
);
endif;
// If our author is enabled, show it
if ( $author ) :
printf( ' <span class="byline">%1$s</span>',
sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%1$s <a class="url fn n" href="%2$s" title="%3$s" rel="author" itemprop="url"><span class="author-name" itemprop="name">%4$s</span></a></span>',
__( 'by','generatepress'),
esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
esc_attr( sprintf( __( 'View all posts by %s', 'generatepress' ), get_the_author() ) ),
esc_html( get_the_author() )
)
);
endif;
}
endif;
and the CSS is:
.posted-on .updated {
display: inline-block;
}
Now there are 3 dates on the source code: published, modified and updated. Why It's so..?
Its supposed to be published and updated only..?
Before adding this snippet google was showing the published date on SERP.
Now it's not showing any dates on search results. My intention is to show last updated date on SERP.
Am I missing something...?
Thank you in advance.
Alan
Hi there,
Have you tried this solution?: https://docs.generatepress.com/article/show-the-updated-post-date/
There shouldn't be any need for PHP.
Let me know :)
Thanks, Tom for your reply...
I was using only this css from this article https://docs.generatepress.com/article/show-the-updated-post-date/ at first with no success as Google was picking the published date only as it is first in the code...
Then I searched further on this forum and FB grp and landed up in this post...
https://woorkup.com/generatepress-review/
Author Brain Jackson using the above snippet on his blog along with that css...
And in other related threads also, some other code snippets have been recommended in this forum along with that css.
So now I am confused. What is the right way...?
I like to show the last updated date in posts as well as in SERP..?
Thank you.
Google says they will show the date that appears on the page in the search results, but they aren't very consistent in our experience.
This is something I'm experimenting with - might be worth trying:
add_filter( 'generate_post_date_output', function() {
$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 = '<meta itemprop="datePublished" content="%1$s">';
$time_string = '<time class="updated" datetime="%3$s" itemprop="dateModified">%4$s</time>' . $time_string;
}
$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">%1$s</span>', // WPCS: XSS ok, sanitization ok.
sprintf( '<a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>',
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
$time_string
)
);
} );
Understood Tom...I will use this snippet along with that css...If I notice any change in SERP for my posts, I will update you...Thanks a lot.
Thanks! :)
Hi Tom
I tested the above two snippets on my different blogs for some time. The snippet provided by you here displays last updated date on posts but in google results, no dates were displayed (neither published nor updated).
The snippet which I got from woorkup site (I think which Brain got from your Github page) works on posts as well as in google results.
I shared this as you told that you are experimenting on this issue.
Thank you
Good to know - thank you!
Hey guys
I tried both codes.
In both cases the date is not displayed on the front end. Yet the time is shown correctly in the source code for google.
I am on latest GP + Premium and latest WP 4.9.
Below is front end source code of Tom's code
<div class="entry-meta">
<span class="posted-on"><a href="https://www.website.net/post-url/" title="12:26" rel="bookmark">
<time class="updated" datetime="2018-12-22T11:33:04+00:00" itemprop="dateModified">22. Dezember 2018
</time>
<meta itemprop="datePublished" content="2018-03-28T12:26:14+00:00">
</a>
</span>
<span class="byline"><span class="author vcard" itemtype="https://schema.org/Person" itemscope="itemscope" itemprop="author">von
<a class="url fn n" href="https://www.website.net/author/user/" title="Alle Beiträge von author anzeigen" rel="author" itemprop="url"><span class="author-name" itemprop="name">Author name</span></a>
</span>
</span>
</div>
And it's not displaying on the site? Can you link us to one of your posts?
Any way I can share the URL with you privately?
Hi there,
you can edit your original topic and change the Site URL :)
I am facing the same issue. Using the code and css shared by Brian Jackson.
Its not showing any date on the blog post. I also tried the css on https://docs.generatepress.com/article/show-the-updated-post-date/
Please help to resolve
Try this function instead of the one Brian suggests:
add_filter( 'generate_post_date_output', function() {
$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" 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">%1$s</span>', // WPCS: XSS ok, sanitization ok.
sprintf( '<a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>',
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
$time_string
)
);
} );
Unfortunately, it is still not working.
I replaced Brian's one with this php in code snippet plug-in and am using the CSS on generate press i.e.
.posted-on .updated {
display: inline-block;
}
.posted-on .updated + .entry-date {
display: none;
}
Have the display post date checked in the Blog post.