Archived topic
Remove date published from html to prevent date from showing in google
21 replies · Started by Aaron on October 15, 2018
Hi there,
I'm wanting to remove the date published item from the HTML to prevent Google from showing dates in the search results.
I have tried disabling all plugins and it still shows, so it must be generated by wordpress or the theme somewhere?
Here is a sample of the HTML from the page containing to code:
<span class="posted-on"><a href="siteremoved" title="5:46 am" rel="bookmark"><time class="updated" datetime="2018-09-23T06:49:41+00:00" itemprop="dateModified">September 23, 2018</time><time class="entry-date published" datetime="2018-02-15T05:46:49+00:00" itemprop="datePublished">February 15, 2018</time></a></span>
So I want to keep the date modified tag, but remove the published date/time.
Hi there,
So you want the modified date to show, but no date to show if it hasn't been modified?
Try this:
add_filter( 'generate_post_date_output', function() {
$time_string = '';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$time_string = '<time class="updated" datetime="%1$s" itemprop="dateModified">%2$s</time>' . $time_string;
}
$time_string = sprintf( $time_string,
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date() )
);
return sprintf( '<a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>',
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
$time_string
);
} );
Let me know :)
Yes, in the code. My objective is to not show published dates in the google search results.
Ill give the code ago and report back.
Hey Tom,
Worked perfectly. Thanks!
You're welcome :)
Hi Tom,
Just a follow up question - is it possible to make the posts always show a "last updated" date just under the title next to the author name? Even if the last time they were updated, was when they were published?
So basically keeping the published date, but naming it updated instead?
At the moment, when I publish a post - there is no date shown under the title. Untill I edit the article again, then it shows the last updated date.
I would like to be able to show the "last updated" date or the "published date" here - whichever is more recent.
Is this possible, without adding the <time class="entry-date published" datetime="2018-02-15T05:46:49+00:00" itemprop="datePublished"> back in?
Sure, try this as your function instead:
add_filter( 'generate_post_date_output', function() {
$time_string = '<time class="entry-date published">%2$s</time>';
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() )
);
return sprintf( '<a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>',
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
$time_string
);
} );
Looks to be the same result - no date shown on a new post, date shown on a modified post.
You'll need to get rid of this CSS:
time.entry-date.published {
display: none;
}
Ah yeah forgot about that! All fixed - thanks!
Actually, now it is showing both the updated and published dates on posts that have been modified.
I have added some text to the code for now so as to not confuse visitors, but ideally would like it just to show the most recent date.
Made one more adjustment above: https://generatepress.com/forums/topic/remove-date-published-from-html-to-prevent-date-from-showing-in-google/#post-707756
Hi. New Generatepress user here.
I am replacing my Genesis theme with Generate press and this is exactly what I'm trying to accomplish.
Where would I put the code?
Would I create a new element? If so, would it be a hook or something else? And where should it go?