- This topic has 7 replies, 3 voices, and was last updated 2 years, 8 months ago by
David.
-
AuthorPosts
-
December 18, 2022 at 4:35 am #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!
Lfunction 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');
December 18, 2022 at 6:04 am #2465893David
StaffCustomer SupportHi 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.December 18, 2022 at 12:31 pm #2466250LoGP
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 🙂
December 18, 2022 at 6:10 pm #2466421Fernando 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.
December 19, 2022 at 1:44 am #2466634LoGP
Hey Fernando,
Thanks for the reply.
– I replaced the code
– I replaced the text of the GB Headline block toplaceholder
But now it outputs the text “placeholder”, not the reading time 🙁
December 19, 2022 at 4:14 am #2466762David
StaffCustomer SupportI updated Fernandos code.
I change this line:
$block_content1 = str_replace( $myreplace1, $myinsert1 , $block_content );
to
$block_content = str_replace( $myreplace1, $myinsert1 , $block_content );
December 19, 2022 at 4:40 am #2466791LoGP
It works!
It shows the correct reading time everywhere. GB Headline block styling works.
You guys are amazing, thanks again! 🙂
-x-
December 19, 2022 at 5:20 am #2466844David
StaffCustomer SupportGlad we could be of help!
-
AuthorPosts
- You must be logged in to reply to this topic.