- This topic has 6 replies, 2 voices, and was last updated 4 years, 1 month ago by
Fernando.
-
AuthorPosts
-
March 17, 2022 at 5:47 pm #2158566
Melvin
Hi,
I’ve followed the instructions here and added estimated reading time to my site successfully.
However, most of my posts are between 45 seconds to 1 minute 20 seconds. As a result, the Estimated Reading Time is always 1 minutes.
Firstly, 1 minutes (instead of the singular minute) is already grammatically incorrect. Secondly, if the result is always 1 minute, it’s as good as not showing.
Is there a way to show the estimated reading time in minutes and seconds? Like 45 seconds, 1 minute 30 seconds, or 2 minutes 10 seconds?
March 17, 2022 at 6:05 pm #2158571Fernando Customer Support
Hi Melvin,
I see. If that’s the case, can you try this code instead and see if it works for you?:
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)); $time = $m . ' minute' . ($m == 1 ? '' : 's') . ', ' . $s . ' second' . ($s == 1 ? '' : 's'); return $time; } add_filter( 'generate_post_date_output', function( $output ) { $output .= '<div class="read-time">Reading time: ' . tu_estimated_reading_time() . '</div>'; return $output; } );Kindly let us know how it goes. 🙂
March 17, 2022 at 6:18 pm #2158577Melvin
Hey Fernando,
Thanks for the quick response. It works!
However, is there a way to change minutes to minute if it’s 0?
Like “0 minute, 42 seconds” instead of “0 minutes, 42 seconds”?
March 17, 2022 at 6:32 pm #2158585Melvin
Hey Fernando,
If it’s too difficult, don’t bother. I’ll just use “m” and “s” instead. Like 0m:42s. No need to stress with singular and plural. 🙂
March 17, 2022 at 6:49 pm #2158591Fernando Customer Support
I see.
Can you try this instead?:
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)); $time = $m . ' minute' . ($m <= 1 ? '' : 's') . ', ' . $s . ' second' . ($s <= 1 ? '' : 's'); return $time; } add_filter( 'generate_post_date_output', function( $output ) { $output .= '<div class="read-time">Reading time: ' . tu_estimated_reading_time() . '</div>'; return $output; } );Kindly let us know how it goes. 🙂
March 17, 2022 at 7:56 pm #2158640Melvin
Hey Fernando,
Yes, it worked perfectly!
Thank you so much for your help. I appreciate you.
Best regards,
Melvin NeoMarch 20, 2022 at 5:05 pm #2161437Fernando Customer Support
You’re welcome Melvin! Glad that worked! Feel free to reach out anytime if you’ll need assistance with anything else. 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.