Archived topic
Show updated post date if it exists in header element
11 replies · Started by George on February 24, 2021
I want to be able to show updated post date in the header element if it exists otherwise I want to show published date. My code in the header element is this:
<h1>{{post_title}}</h1>
<div class="entry-meta entry-meta-container">
<div class="entry-meta-gravatar">
[page_hero_gravatar]
</div>
<div class="entry-meta-info">
<div class="entry-meta-author">
{{post_author}}
</div>
<div class="entry-meta-date">
{{post_date}}
</div>
</div>
</div>
I know there is a shortcode snippet for the updated date for the header element but as I mentioned, I only want to show it if it exists.
I also have:
function post_modified_date() {
return get_the_modified_date();
}
add_shortcode( 'modified_date', 'post_modified_date' );
to show updated date on the header element.
I also have the following CSS:
.entry-meta-date .updated {
display: inline-block;
}
that doesn't do much either.
When I put
.entry-meta-date .updated {
display: inline-block !important;
}
it shows both dates.
Hi there,
You can try this PHP snippet:
function db_modified_time_stamp( $output, $time_string ) {
$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">Published on %2$s</time> ';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$time_string = '<time class="updated" datetime="%3$s" itemprop="dateModified">Updated on %4$s</time> ' . $time_string;
}
// get modified time and published time. Compare the two, If modified date exists then ouput both time strings with modified date
$updated_time = get_the_modified_time( 'U' );
$published_time = get_the_time( 'U' ) + 86400;
if ( $updated_time > $published_time ) {
$time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified" style="display:inline;">Updated on %4$s</time>';
}
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date('F j, Y') ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date('F j, Y') )
);
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
)
);
}
add_shortcode( 'hero_date','db_modified_time_stamp' );
This allows you to use the shortcode [hero_date] to replace {{post_date}}
This automatically displays modified date instead of the post date if it exists.
Yes, it works.
There is an issue, though, not sure if it's expected behavior. When I publish a newly created test post with a previous date than today and preview the post, it says "Updated on [today's date]". Shouldn't it say "Published on [previous date]"?
Hi there,
so the date is set in the past ?
Yes, when a new post is created. I am doing that as a test and it comes out that way.
Actually, I have just noticed that it doesn't actually work. All dates show 23rd of February 2021. What is going on?
Actually, I have just noticed that it doesn’t actually work. All dates show 23rd of February 2021. What is going on?
I've actually tested the same code on a test site prior to posting it and it seems to work as intended.
Example links:
https://dev-generate-press.pantheonsite.io/?p=1651
https://dev-generate-press.pantheonsite.io/?p=1640
Link #1 was posted on February 18. But on the Hero section, you can see that the date displayed is February 25, 2021 which is when I last made any changes on it. You can actually see the difference here: https://share.getcloudapp.com/d5uwgBPG
Link #2 was posted on the same day, February 18. But you can see on the Hero section that the date displayed is "Published on" + Feb 18 as well. That's because I didn't make any updates/changes to it.
https://share.getcloudapp.com/p9u8ep89
Here's a few things:
If you published the post on February 23 and made any updates on it the same day, it would make sense why the updated date is the same as the published date.
If you didn't change anything, it will keep the "Published on" + post date.
If you want to remove the labels, You can check the PHP snippet and remove the Published on and Updated on strings.
Hey hi. I don't have problems with the labels. It's just that all single posts that have different published dates are showing an updated date of February 23rd 2021 in the frontend. What could be causing this? I have not updated all posts at once. I have also deactivated all other scripts and kept only yours. I am attaching a link to the dev site.
I installed a plugin to see the modified dates in the admin and indeed all modified dates have been updated to this date. I will investigate further and get back to you.
Quite tricky to diagnose when we don't see the backend or atleast the published date and the update date indication at the same the on the same page like my demo pages.
If you could provide us temporary site access, perhaps we could help you check things on our end as well.
Let us know what you find. :)
I am closing this topic since the problem is from my end. When I update a post it shows the corrected updated date.
Thank you, again for your help!
Thank you for letting us know. Glad you got it sorted. :)