Site logo

Archived topic

AddingTime to Post Meta

7 replies · Started by Quested on November 6, 2022

Viewing posts 1–8 of 8

Hi awesome people.

Hope you enjoyed your weekend. One of my client want to add Post Time on Archive Posts Meta. Can you guide me how can I achieve this.

Humble Regards

Hi Quested,

To clarify, are you referring to adding an "Estimated Time to read"?

If so, you can add this PHP snippet:

function tu_estimated_reading_time() {
    ob_start();
    $post = get_post();
    $content = $post->post_content;
    $wpm = 300; // How many words per minute.

    $clean_content = strip_shortcodes( $content );
    $clean_content = strip_tags( $clean_content );
    $word_count = str_word_count( $clean_content );
    $time ='<div class="read-time"><span class="gp-icon">•</span>Reading time: '. ceil( $word_count / $wpm ) . ' minutes'. '</div>';

    return  $time; 
    return ob_get_clean();
}

add_shortcode('tu_reading_time', 'tu_estimated_reading_time');

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

Then, add a Shortcode Block to your Block Element - Content Template, and add this code: [tu_reading_time]

Thank you for relying back.
Actually I want to know how can I add time on meta post.
Example:- 6:30 hours ago

Hi there,

Do you mean you don't want to show the post date, but only the xx hours ago or xx days ago?

Or do you just simply want to add the time together with the post date?

I want to show post date as (October 11, 2022). But on a Custom Post Type (Dialy Posts) I want to have hours ago option.
Example: 9:10 hours ago

You can add an additional CSS class to the headline block which represnet the date, eg. my-date, then add this PHP snippet:

add_filter( 'render_block', function( $block_content, $block ) {
    if (  ! empty( $block['attrs']['className'] ) && 'my-date' ===  $block['attrs']['className'] )  {
    
      $my_date = sprintf( esc_html__( '%s ago', 'textdomain' ), human_time_diff(get_the_time ( 'U' ), current_time( 'timestamp' )) );
      $block_content = sprintf(
			'<time class="entry-date published" datetime="%1$s">%2$s</time>',
			esc_attr( get_the_date() ),
			$my_date
		);
    }

    return $block_content;
}, 10, 2 );

You guys are awesome.

Thank you so much for your help.

You are welcome :)

This archived topic is closed to new replies.