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

Home Forums Support [Resolved] Show Published Date If Modified Date is not available

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

Viewing 8 posts - 16 through 23 (of 23 total)
  • Author
    Posts
  • #1135755
    Suraj Katwal

    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?

    #1135800
    David
    Staff
    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 Katwal

    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 );
    #1135837
    Suraj Katwal

    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

    #1142262
    Suraj Katwal

    Any Update?

    #1142456
    David
    Staff
    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 );
    #1481738
    Franklin

    where to find the file to make this change?

    I can’t find it in functions.php

    #1481869
    David
    Staff
    Customer Support

    Hi there,

    that code is PHP – this article explains:

    https://docs.generatepress.com/article/adding-php/

    However things have changed since this topic. I have replied directly to your topic to answer this.

Viewing 8 posts - 16 through 23 (of 23 total)
  • You must be logged in to reply to this topic.