Site logo

[Resolved] AddingTime to Post Meta

Home Forums Support [Resolved] AddingTime to Post Meta

Home Forums Support AddingTime to Post Meta

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #2403822
    Quested

    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

    #2404082
    Fernando
    Customer Support

    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]

    #2405082
    Quested

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

    #2405257
    Ying
    Staff
    Customer Support

    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?

    #2405388
    Quested

    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

    #2405500
    Ying
    Staff
    Customer Support

    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 );
    #2405526
    Quested

    You guys are awesome.

    Thank you so much for your help.

    #2405531
    Ying
    Staff
    Customer Support

    You are welcome 🙂

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