• Resolved JJCoding

    (@janjacobs)


    Hi,

    Is there any chance of adding support for adding the shortcode to a default Gutenberg Query Loop? Right now it seems to only get the reading time for 1 article (probably the first) and then repeats this for every post in the loop instead of calculating it again for every item in the loop.

    Kind regards

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter JJCoding

    (@janjacobs)

    Fixed this by creating my own filter instead of using a plug-in.

    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 = $word_count / $wpm;
    
          		if($time > 1) {
                  $suffix = 'minutes';
                } else {
                  $suffix = 'minute';
                }
          
          		if($time < 1) {
                	$time = '< 1';
                } else {
               		$time = ceil( $word_count / $wpm );
                }
          
                $block_content = '<span class="read-time">Reading time: ' . $time .' '. $suffix . '</span>';
        }
        
        return $block_content;
    }, 10, 2 );

    Credit to: https://generatepress.com/forums/topic/add-reading-time-on-posts-after-the-author-name/

    • This reply was modified 1 year, 11 months ago by JJCoding.

    I ran into this issue as well. I slapped together this plugin to address it. You just need the build folder and query-loop-shortcode.php:

    KeanuTang/query-loop-shortcode: WordPress Plugin for Gutenberg Shortcode Block to Work Within Query Loops (github.com)

    Plugin Author Jason Yingling

    (@yingling017)

    Thanks for sharing. I’ll take a look at options for implementing to be able to get the post ID from within a query loop when embedding the shortcode in the post template.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Gutenberg Query Loop’ is closed to new replies.