Site logo

Archived topic

Correct way to show only Modified Date on page hero

6 replies · Started by Emmanuel on June 27, 2020

Viewing posts 1–7 of 7

Hi Tom,

I have read through the forum and managed to display Updated Date on Page Hero using this shortcode:

I have also read this post: https://docs.generatepress.com/article/generate_post_date_output/#only-show-updated-date and added the code in my functions.

Then I read here that one should only display the modified date in order for Google to display it in results. My test in structured data shows both updated and published date.

How can I completely disable published date so that Google could pick it up? He did not mention how he solved it.

Thanks

Hi there,

Is this issue still happening?

I'm only seeing updated date now.

Hi Leo,
Yes, it is still happening.

On Google, it is showing the published dates for articles rather than the updated date.

When I use this code provided by Tom the date is not displayed.

Hello,

So I added this code that Tom Provided:

function display_modified_date_shortcode () {
    $mod_date = get_the_modified_date();
    if ( get_the_date() !== $mod_date ) {
        return sprintf(
            'Updated On <time class="updated" datetime="%1$s" itemprop="dateModified">%2$s</time>',
            esc_attr( get_the_modified_date( 'c' ) ),
            esc_html( $mod_date )
        );
    }
}
add_shortcode( 'modified_date', 'display_modified_date_shortcode' );

But the date does not display because of this CSS

.page-hero time.updated {
    display: none;
}

Another thing I noticed is that if a post has not been updated the Updated Date will not show completely.

Hi there,

You're better off using the {{post_date}} template tag and doing this:

add_filter( 'generate_page_hero_post_date', function() {
    $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';

    $updated_time = get_the_modified_time( 'U' );
    $published_time = get_the_time( 'U' ) + 1800;

    if ( $updated_time > $published_time ) {
        $time_string = '<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;
} );

Hi Tom,

Thanks. This works as expected.

Regards

You're welcome :)

This archived topic is closed to new replies.