Site logo

Archived topic

Show posted Updated date automatically

5 replies · Started by Techibytes Media on September 27, 2022

Viewing posts 1–6 of 6

so I bought the premium theme but at the moment downgraded to the free theme because of ezoic errors. So I want to know how you can help me in showing article's edited date while the original date remains.

Hello there,

You can try adding a filter like this:

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

if ( get_the_date() !== get_the_modified_date() ) {
	$time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">Updated: %4$s</time>
	<time class="entry-date published" datetime="%1$s" itemprop="datePublished">Published: %2$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 );

Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets

Thanks Fernando,

Would it be possible to use that snippet within a header element? I'm currently using {{post_date}} but I'd like to automatically show the modified date instead if one exists.

Thanks!

Thanks Ying! I was able to get the published/updated date working within the post meta template. Is it possible to pull the template into a header (hero image) element?

This archived topic is closed to new replies.