Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Reading time with GenerateBlocks

  • Hello,

    Is there any way to place the reading time of the blog post in the generablocks query loops, and inside the blog post as such?

  • Hi there,

    Are you using a plugin to add the reading time? Does the plugin offer a block or a shortcode?

  • Hello Ying,

    I am not using any plugin that has that functionality.

  • Hi there,

    I see. Could you share the link to where we can find your Query Loop?

  • Hello Fernando,

    The site is this: https://bkdev.site/

    I have been searching and found a snippet to add something similar with Rank Math.

    However it only works within the blog post, and not in the query loops.

    This is the script:

    add_shortcode( 'rm_reading_time', 'define_custom_shortcode_function' );
    function define_custom_shortcode_function() {
        global $post;
        $content = wp_strip_all_tags( $post->post_content );
        $words_per_minute = 250;
        $time = ceil(str_word_count($content)/$words_per_minute);
        return strval($time);
    }
  • Add a GB Headline Block for the Reading time in your Query Loop.

    Then, type the shortcode [rm_reading_time] inside this Headline, add a CSS class to the headline, eg. get-reading-time. Adding Custom Classes: https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/

    Add this PHP code to render the headline:

    add_filter( 'render_block_generateblocks/headline', function( $block_content, $block ) {
        if ( 
          !is_admin() && 
          ! empty( $block['attrs']['className'] ) && 
          strpos( $block['attrs']['className'], 'get-reading-time' ) !== false 
        ) {
          $post_id = get_the_ID();
          $block_content = do_shortcode('[rm_reading_time]');		
        }
      
        return $block_content;
     }, 10, 2 );

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

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