[Resolved] Show Published Date If Modified Date is not available

Home Forums Support Show Published Date If Modified Date is not available

This topic contains 20 replies, has 4 voices, and was last updated by  David 3 weeks, 4 days ago.

Viewing 6 posts - 16 through 21 (of 21 total)
  • Author
    Posts
  • #1135755

    Suraj

    I have an issue with the code too.
    Let’s say
    Case 1. When I have published the post today, it should show something like this
    January 18, 2020 by Author Name
    but it’s showing Last Updated on: January 18, 2020 by Author Name
    Case 2. When I actually updated the post, it is working fine. showing
    Last Updated on: January 18, 2020 by Author Name

    Is it possible to get like in case 1?

    SEO, WordPress Optimization, Blogging Tutorials.
    GeneratePress Tutorial: https://www.wplogout.com

    #1135800

    David Customer Support

    Hi there,

    use the original code provided here:

    https://docs.generatepress.com/article/generate_post_date_output/#only-show-updated-date

    Then update this line for the update-date:

    $time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">%4$s</time>';

    to:

    $time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">Last Updated on: %4$s</time>';

    #1135836

    Suraj

    It’s not working

    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' ) ) {
            $time_string = '<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 );

    SEO, WordPress Optimization, Blogging Tutorials.
    GeneratePress Tutorial: https://www.wplogout.com

    #1135837

    Suraj

    It’s not working

    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' ) ) {
            $time_string = '<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 );

    Everything is fine but when I hit the publish button for the first time it’s showing Last Updated Date Text.
    Thank You

    SEO, WordPress Optimization, Blogging Tutorials.
    GeneratePress Tutorial: https://www.wplogout.com

    #1142262

    Suraj

    Any Update?

    SEO, WordPress Optimization, Blogging Tutorials.
    GeneratePress Tutorial: https://www.wplogout.com

    #1142456

    David Customer Support

    Hi there,

    sorry we missed your replies – somehow they slipped through our system.
    Try this code – it will compare date strings – so should only show updated if the there is a days difference:

    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 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 );
Viewing 6 posts - 16 through 21 (of 21 total)

You must be logged in to reply to this topic.