Archived topic
Hide modified date if is the same as posted date
7 replies · Started by Ingrid on March 18, 2020
Hello!
First of all I want to say I love Generate Press,its an awesome theme!
So, I want to show the modified date in my posts, I did that by using the CSS I found in this post
But if I post something and modify it in the same day it shows like this:
Modified on 18 march 2020 / Posted on 18 march 2020
is it possible to hide the modified date if the posted date is the same?
I am using the latest Generate Press premium
Thanks in advance!
Hi there,
Can you try this instead?
https://docs.generatepress.com/article/show-the-updated-post-date/
Let me know :)
I have been using this code in my site
add_filter( 'generate_post_date_output', function( $output, $time_string ) {
$time_string = 'Published on: %2$s';
if ( get_the_date() !== get_the_modified_date() ) {
$time_string = 'Last Updated on: %4$s';
}
$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( '%s ',
$time_string
);
}, 10, 2 );
It will show the Published on: date if updated the content on the same day and Last Update on: if you update on another day.
I think David shared this code somewhere and I have implemented it on my blog.
This code helps a lot but I would want to show the published date too, like this:
published on 10 march 2020 / updated on 18 march 2020
and if the date of publishing and editing are the same then just the published date:
published on 10 march 2020
Hi there,
It shouldn't print the updated date unless it's different than the published date. Are you using a function/CSS as of right now? Any chance you can link us to a post?
Hi Tom,
Here is what David Replied to me Last Updated Date and here is what I have been using in my GeneratePress to Display Last Updated Date and Published Date
Thank you
Hi guys, I modified a bit the code that Suraj Katwal linked to, it worked for me! Now it shows the published and modified date,and if the post was modified the same day is shows just the published date:
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_date() !== get_the_modified_date() ) {
$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">published on: %2$s</time> / <time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">Last Updated on: %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 );
Thanks a lot for the help!
Awesome, thanks for sharing your code! :)