Site logo

Archived topic

Last Updated Date Displaying as Yesterdays Date for All Posts

17 replies · Started by Scott on January 28, 2020

Viewing posts 1–15 of 18

Hi there

I've followed the instructions in the discussion below, and now have Last Updated dates displaying in my Page Hero under my H1.

https://generatepress.com/forums/topic/how-to-show-the-last-updated-post-date-when-using-page-header-edit-element/

My first question relates to the dates themselves. They are all displaying dates from the past 2 days, even if no change has been made to these posts. What triggers the [modified_date] to be updated?

Second question:

I'm using the below CSS to change the font size.

.hero-meta {
font-size: 18px;
}

It is working well for desktop but is too large on mobile. I've tried adding the below with no success:

@media (max-width: 768px)

Hi David

Yes. Here's what I've done:

Add the below PHP:

function display_modified_date_shortcode () {
$mod_date = get_the_modified_date();
if ( get_the_date() !== $mod_date ) {
return 'Last Updated: ' . $mod_date;
}
}
add_shortcode( 'modified_date', 'display_modified_date_shortcode' );

Added the below under the H1 in my Page Hero Header element:

[modified_date]

Added the below CSS to edit the text size:

.hero-meta {
font-size: 18px;
}

Instead of introducing new code, I would just use the standard {{post_date}}

Then you can tell that template tag to show the updated date with a filter:

add_filter( 'generate_page_hero_post_date', function() {
    $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 = 'Last Updated: <time class="entry-date-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 $time_string;
} );

Let me know :)

Hi Tom

Thanks for your reply.

So, I've replaced the previous PHP with the code in your message above.

I've then replaced [modified_date] under my H1 with {{post_date}}.

The result is it now displays "Last Updated:" but with no date.

Any ideas?

Hmm, that means the WP core function get_the_modified_date() isn't doing anything on your site.

Do you have any other custom functions added?

Nope, this is it as far as I'm aware.

Are your date and time format configured in "Settings > General"?

Yep. And they were appearing OK when applying the earlier code from David, it's just the dates didn't seem correct.

OK, so using the updated code, the date is now displaying under the H1. However, it seems to have the same issue as earlier, where all dates are very recent (last few days), even if the post hasn't been modified. Would something like the sidebar changing due to a related post widget trigger the modified date? Or does the content of the post itself need to change?

Also, I've enabled post date in archive in Layout > Blog and interestingly this displays the original post date, not the modified date.

Are you sure the posts haven't been modified? What's the latest modified date for them in the Dashboard?

These are all core WordPress functions, so if they're malfunctioning it means something on your installation/server is configured wrong.

Give this a shot:

@media (max-width: 768px) {
    .hero-meta {
        font-size: 18px;
    }
}

Hmm yeah that's what I had tried already. Just tried again with no success.

So I have this in my Page hero element:

<br>
<br>
<h1>{{post_title}}</h1>
{{post_date}}

And this in my Additional CSS:

@media (max-width: 768px) {
.hero-meta {
font-size: 8px;
}
}

Does that look right?

This archived topic is closed to new replies.