[Resolved] Add Reading time on posts after the author name

Home Forums Support [Resolved] Add Reading time on posts after the author name

Home Forums Support Add Reading time on posts after the author name

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #2431017
    Rostyslav

    Hello!

    How can I add a Reading time on posts after the author name?

    https://ibb.co/Y8hRQ7W

    #2431086
    David
    Staff
    Customer Support

    Hi there,

    1. use the Block Element – Post Meta to create your meta:

    https://docs.generatepress.com/article/block-element-post-meta-template/

    1.1 For the reading time, just add a Headline block, withs some static text eg. Time and in Advanced > Addtional CSS Class(es) add: reading-time

    2. Add this PHP Snippet to your site to swap that headline for an estimated reading time:

    
    add_filter( 'render_block', function( $block_content, $block ) {
    	
        if (
            !is_admin() 
            && ! empty( $block['attrs']['className'] )
            && 'reading-time' === $block['attrs']['className']
            ){
                $post = get_post();
                $content = $post->post_content;
                $wpm = 250; // How many words per minute.
                $clean_content = strip_shortcodes( $content );
                $clean_content = strip_tags( $clean_content );
                $word_count = str_word_count( $clean_content );
                $time = ceil( $word_count / $wpm );
    
                $block_content = '<span class="read-time">' . $time . ' minutes</span>';
        }
        
        return $block_content;
    
    }, 10, 2 );
    #2431105
    Rostyslav

    Thanks for answering so fast.

    I want to keep the built-in generatepress date and author metas. Is there any code that adds the reading time next to that default metas : https://ibb.co/Y8hRQ7W

    Thanks!

    #2431118
    David
    Staff
    Customer Support

    Are you already using any custom functions on the post meta ?

    #2431486
    Rostyslav

    Yes:

    /**If you want to show the updated date if it exists, otherwise the publish date**/
    add_filter( 'generate_post_date_show_updated_only', '__return_true' );
    /**añadir actualizado el antes de la fecha**/
    add_filter( 'generate_post_date_output','tu_add_to_post_date' );
    function tu_add_to_post_date( $output ) {
        return '<span class="date-label">Actualizado el </span>' . $output;
    }
    #2432054
    Fernando
    Customer Support

    Hi Rostyslav,

    I see.

    You can try adding this PHP snippet:

    function tu_estimated_reading_time() {
        $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 );
    	$m = floor($word_count / 200);
        $s = floor($word_count % $wpm / ($wpm / 60));
    	if($m < 1) {
    		$time = $s . ' second' . ($s <= 1 ? '' : 's');
    	} else {
    		$time = $m . ' minute' . ($m <= 1 ? '' : 's') . ', ' . $s . ' second' . ($s <= 1 ? '' : 's');
    	}
    	
        return $time;
    }
    
    add_filter( 'generate_post_author_output', function( $output ) {
        $output .= '<span class="read-time">Reading time: ' . tu_estimated_reading_time() . '</span>';
    
        return $output;
    } );

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

    #2438455
    Rostyslav

    Hi, thanks!

    How can I display just minutes without seconds?

    Thanks!

    #2438746
    David
    Staff
    Customer Support

    Change the code to:

    
    function tu_estimated_reading_time() {
        $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 );
        $m = ceil( $word_count / 200 );
    	if($m < 2) {
    		$time = '1 min'
    	} else {
    		$time = $m . ' min';
    	}
    	
        return $time;
    }
    
    add_filter( 'generate_post_author_output', function( $output ) {
        $output .= '<span class="read-time">Reading time: ' . tu_estimated_reading_time() . '</span>';
    
        return $output;
    } );
    #2439727
    Rostyslav

    Thanks

    #2440581
    David
    Staff
    Customer Support
Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.