• Hi,
    Regarding post date, I want to show “Published On:” for never updated post.
    And “Updated On:” for updated post instead of Published On.
    Can you please help me with it?
    Thanks,
    Rasel

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter kzrasel

    (@kzrasel)

    Is there anybody to reply me ??

    Theme Author Tom

    (@edge22)

    Hi there,

    This should help: https://docs.generatepress.com/article/show-the-updated-post-date/

    To add Published on:, you would do this as well:

    .posted-on .published:before {
        content: "Published on: ";
    }

    Please allow up to 24 hours for support, especially as this is our free support area.

    Thanks!

    Thread Starter kzrasel

    (@kzrasel)

    Hi there,

    Thanks a lot for reply.
    Below custom css code is working fine for me.

    .posted-on .updated {
        display: inline-block;
    }
    .posted-on .updated + .entry-date {
        display: none;
    }
    .entry-date:before {
        content: "Published On: ";
    }
    .posted-on .updated:before {
        content: "Updated On: ";
    }

    But, I want same effect from theme function (instead of adding code on customizer css).

    Can you help me on that?

    Again thanks a lot for free support ??

    Elvin

    (@ejcabquina)

    Hi there,

    You can use the generate_post_date_output filter for this if you want to process things server side.

    How do you want the date to behave? Do you want to display only the modified date in place of the post date its available?

    If so, 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;
    	}
    
        // Check modified date is newer thant published
    
        $updated_time = get_the_modified_time( 'U' );
        $published_time = get_the_time( 'U' ) + 86400;
    
        // If modified date exists then output both time strings with modified date
        
        if ( $updated_time > $published_time ) {
            $time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished" style="display: none;">Published on: %2$s</time>
            <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">%s</span> ',
            $time_string
        );
    }
    
    add_filter( 'generate_post_date_output', db_modified_time_stamp, 20, 2);
    Theme Author Tom

    (@edge22)

    I actually think the CSS you’re using above is the better, more performant method.

    Which part do you not want to use CSS for? Hiding the published date? Adding the text?

    Thread Starter kzrasel

    (@kzrasel)

    Hi,

    Thank you for reply.

    .posted-on .updated {
        display: inline-block;
    }
    .posted-on .updated + .entry-date {
        display: none;
    }
    .entry-date:before {
        content: "Published On: ";
    }
    .posted-on .updated:before {
        content: "Updated On: ";
    }

    Actually I want same effect what showing after adding above code to customizer css.

    But, I just want this effect directly from theme function (instead of/ not by adding code in customizer)

    As example below code I was using in my another theme for ref. effect:

    <time class="post-date" datetime="<?php echo esc_attr(get_the_date(DATE_W3C)); ?>">
                <?php echo get_the_date() == get_the_modified_date() ? 
                 'Published on: ' .esc_html(get_the_date()) : 'Updated on: '. esc_html(get_the_modified_date()); ?>
                 </time>

    Hope you got me ??

    Btw, as you saying the customizer css method is more performant, also which method is better for SEO? As google like newest content.

    Thanks
    Rasel

    Theme Author Tom

    (@edge22)

    As far as I know, it doesn’t make a difference in regards to SEO.

    If you want to replace the published date with the updated date (while completely removing the updated date), you should use this filter: https://docs.generatepress.com/article/generate_post_date_show_updated_only/

    As for adding the text, try this:

    add_filter( 'generate_inside_post_meta_item_output', function( $output, $item ) {
        if ( 'date' === $item ) {
            $updated_time = get_the_modified_time( 'U' );
            $published_time = get_the_time( 'U' ) + 1800;
    
            if ( $updated_time > $published_time ) {
                return 'Updated on: ';
            } else {
                return 'Posted on: ';
            }
        }
    
        return $output;
    }, 10, 2 );
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Show Post date and updated date’ is closed to new replies.