Site logo

[Resolved] How to display the Time a post was published?

Home Forums Support [Resolved] How to display the Time a post was published?

Home Forums Support How to display the Time a post was published?

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1884316
    nomadiceman

    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?

    #1884318
    Elvin
    Staff
    Customer Support

    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. 😀

    #1884322
    nomadiceman

    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

    #1884333
    Elvin
    Staff
    Customer Support

    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);
    #1884339
    nomadiceman

    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

    #1884391
    Elvin
    Staff
    Customer Support

    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);
    #1884393
    nomadiceman

    perfect thank you

    #1888618
    Elvin
    Staff
    Customer Support

    No problem. glad to be of any help. 😀

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