Archived topic
How to add "Published on" and "Updated on" without CSS?
5 replies · Started by NrH on June 6, 2022
Hello, I have two questions, but the most important one is how can I add the two labels "Published on" and "Updated on" before the Posted on and Updated on date times in the blogposts and archives without using pseudoelements and CSS?
And the second question is how can I show the Updated on date time on the blogposts/archives instead of the "Published on" date time? Thank you in advance!
Hi there,
are you wanting to display both the Updated on and Publish on dates ? Or just one of them ?
And are you a GenerateBlocks user ?
Hi! Not a GenerateBlocks user, still using sections
I want to display the Updated on-date if it exists, and if not Published on, but I also want to display a text before these dates that says "Published on ..." ahead of the published on date, and "Updated on" ahead of the updated on date, without using CSS if that'd be possible?
Thank you again!
Try this PHP Snippet:
add_filter( 'generate_post_date_output', function( $output, $time_string ) {
$time_string = '<span class="date-label">Published on: </span><time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';
// 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 ouput both time strings with modified date
if ( $updated_time > $published_time ) {
$time_string = '<span class="date-label">Updated on: </span><time class="entry-date updated-date" 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 sprintf( '<span class="posted-on">%s</span> ',
$time_string
);
}, 10, 2 );
Awesome works!! Thank you so much!
Glad to hear that!