Archived topic
How to display the Time a post was published?
7 replies · Started by nomadiceman on August 4, 2021
I've got the meta date set up which is great. But I would like to include the Time a post was posted
How can I do that?
Hi there,
Any specific layout on how you want it to be displayed? Do you want both the publish date and the update date to display at the same time?
Let us know. :D
I was just thinking of having the time after the date as its a news site
something like this www.arabianbusiness.com/real-estate/466741-dubai-broker-sees-surge-in-british-property-buyers-amid-uaes-latest-expat-incentives
Ah my bad, I misread the question.
Try this PHP filter.
add_filter( 'generate_post_date_output', function($post_date){
if( is_singular() ){
$post_date = sprintf(
'<time class="entry-date published" datetime="%1$s"><span class="entry-date">%2$s</span><span class="time">%3$s</span></time>',
esc_attr( get_the_date( 'c', $id ) ),
esc_html( get_the_date( 'F j, Y', $id ) ),
esc_html( get_the_time( ' g:i A ', $id ) )
);
return $post_date;
}
return $post_date;
},15,2);
Ive added that snippet and cleared the caches but the Time doesn't show.
You can see in the supplied link. The date published shows, but no time
Ah I see. You're using block element's dynamic post meta.
In that case, the snippet won't work because that's for the theme default post date output.
That said, try this:
If it's only for the published date, try this PHP snippet:
add_filter( 'generate_dynamic_element_text', function( $post_date, $block ){
if ( 'post-date' === $block['attrs']['gpDynamicTextType'] && empty($block['attrs']['gpDynamicDateType']) ) {
$post_date = sprintf(
'<time class="entry-date updated-date" datetime="%1$s">%2$s</time>',
esc_attr( get_the_date( 'c', $id ) ),
esc_html( get_the_date( 'F j, Y g:i A', $id ) )
);
}
return $post_date;
},15,2);
If you want the modified date to have time as well, try this:
add_filter( 'generate_dynamic_element_text', function( $post_date, $block ){
if ( 'post-date' === $block['attrs']['gpDynamicTextType'] && empty($block['attrs']['gpDynamicDateType']) ) {
$post_date = sprintf(
'<time class="entry-date updated-date" datetime="%1$s">%2$s</time>',
esc_attr( get_the_date( 'c', $id ) ),
esc_html( get_the_date( 'F j, Y g:i A', $id ) )
);
} if ( 'post-date' === $block['attrs']['gpDynamicTextType'] && 'updated-date' === $block['attrs']['gpDynamicDateType'] ) {
$post_date = sprintf(
'<time class="entry-date updated-date" datetime="%1$s">%2$s</time>',
esc_attr( get_the_modified_date( 'c', $id ) ),
esc_html( get_the_modified_date( 'F j, Y g:i A', $id ) )
);
}
return $post_date;
},15,2);
perfect thank you
No problem. glad to be of any help. :D