Site logo

[Resolved] Reading time in ‘Query Loop’ (latest posts)

Home Forums Support [Resolved] Reading time in ‘Query Loop’ (latest posts)

Home Forums Support Reading time in ‘Query Loop’ (latest posts)

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #2465828
    LoGP

    Hey guys,

    I’m using the reading time code + shortcode provided here in the GP forums (code below).

    It works fine on posts and archives. But when I add it on a page within a Query Loop that shows the latest posts, it shows the same (incorrect) reading time for all latest posts.

    It seems to grab the reading time of the page it’s being used on with the Query Loop, instead of the reading time of the specific latest posts.

    How can I fix this?

    Thanks!
    L

    function tu_estimated_reading_time() {
        ob_start();
        $post = get_post();
        $content = $post->post_content;
        $wpm = 400; // 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 ) . ' min read'. '';
    
        return  $time; 
        return ob_get_clean();
    }
    
    add_shortcode('tu_reading_time', 'tu_estimated_reading_time');
    #2465893
    David
    Staff
    Customer Support

    Hi there,

    Shortcodes won’t work. Instead do this:

    1. remove the PHP Snippet you have and add this instead:

    
    function tu_estimated_reading_time() {
        ob_start();
        $post = get_post();
        $content = $post->post_content;
        $wpm = 400; // 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 ) . ' min read'. '';
    
        return  $time; 
        return ob_get_clean();
    }
    add_filter( 'render_block', function( $block_content, $block ) {
    	
        if (
            !is_admin() 
            && ! empty( $block['attrs']['className'] )
            && 'reading-time' === $block['attrs']['className']
            ){
                $block_content = '<span class="read-time">— ' . tu_estimated_reading_time() . ' read</span>';
        }
        
        return $block_content;
    
    }, 10, 2 );

    2. In your Query Loop block or wherever you want the reading time displayed:
    2.1 Add a GB Headline block
    2.2 Give it some static text eg. “reading time” and style the block accordingly
    2.3 In Advanced > Additional CSS Class(es) add: reading-time

    Any block with the reading-time class will bow get updated to return the reading time.

    #2466250
    LoGP

    Hey David,

    Thanks! It works and the correct reading time is shown.

    Only thing is that it strips the GB Headline block styling. It only outputs the <span class="read-time"> tags and the reading time in between these tags.

    I can simply style it via the Customizer/Additional CSS, and then it’s all good. But I think your intention with the code was to keep the GB Headline block styling for optimal customization 🙂

    #2466421
    Fernando
    Customer Support

    I see. If that’s the case replace the code above with this:

    function tu_estimated_reading_time() {
        ob_start();
        $post = get_post();
        $content = $post->post_content;
        $wpm = 400; // 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 ) . ' min read'. '';
    
        return  $time; 
        return ob_get_clean();
    }
    add_filter( 'render_block', function( $block_content, $block ) {
        if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'reading-time' ) !== false ) {
    	$myreplace1 = 'placeholder';
    	$myinsert1 = tu_estimated_reading_time();
            $block_content = str_replace( $myreplace1, $myinsert1 , $block_content );	
        }
    
        return $block_content;
    }, 10, 2 );

    Then, replace the text of the GB Headline block to placeholder.

    Let us know how it goes.

    #2466634
    LoGP

    Hey Fernando,

    Thanks for the reply.

    – I replaced the code
    – I replaced the text of the GB Headline block to placeholder

    But now it outputs the text “placeholder”, not the reading time 🙁

    #2466762
    David
    Staff
    Customer Support

    I updated Fernandos code.

    I change this line:

    $block_content1 = str_replace( $myreplace1, $myinsert1 , $block_content );

    to

    $block_content = str_replace( $myreplace1, $myinsert1 , $block_content );

    #2466791
    LoGP

    It works!

    It shows the correct reading time everywhere. GB Headline block styling works.

    You guys are amazing, thanks again! 🙂

    -x-

    #2466844
    David
    Staff
    Customer Support

    Glad we could be of help!

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