- This topic has 9 replies, 3 voices, and was last updated 4 years, 6 months ago by Tom.
-
AuthorPosts
-
September 23, 2019 at 9:10 am #1016846Carson
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!
September 23, 2019 at 9:45 am #1016879LeoStaffCustomer SupportHi there,
Give this snippet a shot:
https://docs.generatepress.com/article/generate_post_date_output/#only-show-updated-dateYou will also need the conditional tag to target specific tag only:
https://codex.wordpress.org/Conditional_Tags#A_Tag_PageLet me know if this helps π
September 23, 2019 at 10:05 am #1016908CarsonThanks, 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. π
September 23, 2019 at 3:51 pm #1017139TomLead DeveloperLead DeveloperHi 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?September 23, 2019 at 5:17 pm #1017165CarsonThis 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!!!
September 24, 2019 at 9:14 am #1017805TomLead DeveloperLead DeveloperLet’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 π
September 24, 2019 at 9:39 am #1017828CarsonWorks 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!
September 24, 2019 at 4:42 pm #1018152TomLead DeveloperLead DeveloperTry 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 π
September 25, 2019 at 2:16 am #1018390CarsonAmazing, thank you very much Tom!!!!
September 25, 2019 at 9:48 am #1018826TomLead DeveloperLead DeveloperYou’re welcome π
-
AuthorPosts
- You must be logged in to reply to this topic.