Archived topic
Remove datePublished and Time from Single Post
9 replies · Started by Carson on September 23, 2019
Hi,
I am looking for a solution to remove datePublished and the time from single posts, so all that shows is "datetime="2019-08-28" in the source code and the date is pulled from when it was last modified.
I already got the CSS in place to display dateModified (which I want to show) and hide datePublished on the front-end, but datePublished still shows up in the source code.
Is it possible to do this based on tags? Meaning that it only removes the datePublished, if a specific tag is set for the post? Could you also remove the link from the date that is shown on page (globally)?
Thanks!
Hi there,
Give this snippet a shot:
https://docs.generatepress.com/article/generate_post_date_output/#only-show-updated-date
You will also need the conditional tag to target specific tag only:
https://codex.wordpress.org/Conditional_Tags#A_Tag_Page
Let me know if this helps :)
Thanks, that removed the datePublished and also removed the link.
Now I just need to remove the time, is there a filter for that as well?
Also, I don't manage to combine the conditional tag and the filter for the published date.
I think I'd need this:
has_tag( array( 'sharp', 'mild', 'extreme' ) )
Could you help me out here please. :)
Hi there,
Try this:
add_filter( 'generate_post_date_output', function( $output, $time_string ) {
if ( has_tag( array( 'sharp', 'mild', 'extreme' ) ) ) {
$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">%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
);
}
return $output;
}, 10, 2 );
Are you wanting to remove the datetime part as well?
This works great with the tags, thanks a lot!
No I don't want to remove the datetime part. The only thing that's left is that I want the time removed from datetime
So like this
<time class="entry-date updated-date" datetime="2019-09-22" itemprop="dateModified">September 22, 2019</time>
instead of this
<time class="entry-date updated-date" datetime="2019-09-22T00:10:40+00:00" itemprop="dateModified">September 22, 2019</time>
it can still say datetime though.
I am looking at the format from Wirecutter as a reference: thewirecutter.com/reviews/the-best-universal-remote-control/
PS: I use this snippet globally to remove the link from the date:
add_filter( 'generate_post_date_output', function( $output, $time_string ) {
printf( '<span class="posted-on">%s</span>',
$time_string
);
}, 10, 2 );
Apparently it causes issues with your snippet. You snippet works perfectly until I activate the one above, then it shows the datePublished again.
I need the link removed globally, the time removed globally, but the published date only removed based on tags.
Thanks for the amazing support!!!
Let's try this:
add_filter( 'generate_post_date_output', function( $output, $time_string ) {
if ( has_tag( array( 'sharp', 'mild', 'extreme' ) ) ) {
$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">%4$s</time>';
}
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'Y-m-d' ) ),
esc_html( get_the_date() ),
esc_attr( get_the_modified_date( 'Y-m-d' ) ),
esc_html( get_the_modified_date() )
);
return sprintf( '<span class="posted-on">%s</span> ',
$time_string
);
}
return sprintf( '<span class="posted-on">%s</span>',
$time_string
);
}, 10, 2 );
Let me know :)
Works perfectly! One minor thing, could you make the time remove part (datetime="2019-09-22") global, like you did with the link removal? Other than that it's perfect! Thanks!
Try this:
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' ) ) {
if ( has_tag( array( 'sharp', 'mild', 'extreme' ) ) ) {
$time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">%4$s</time>';
} else {
$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( 'Y-m-d' ) ),
esc_html( get_the_date() ),
esc_attr( get_the_modified_date( 'Y-m-d' ) ),
esc_html( get_the_modified_date() )
);
return sprintf( '<span class="posted-on">%s</span>',
$time_string
);
}, 10, 2 );
Let me know :)
Amazing, thank you very much Tom!!!!
You're welcome :)