Archived topic
Add reading time code
22 replies · Started by alexis on September 28, 2022
I use dynamic data on the title or image element. These are pages that I will search manually. I can't do it with query loop because the pages I need are custom. I would have to exclude many pages. It's a "semantic cocoon" type of mesh
You'll need a new Shortcode. Try adding this:
function get_estimated_reading_time($attr) {
ob_start();
$options = shortcode_atts( array(
'my-id'=> '1'
), $attr);
$post = get_post($options['my-id']);
$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 = ceil( $word_count / $wpm );
echo '<div class="read-time"><span>Temps de lecture </span>' . $time . ' min</div>';
return ob_get_clean();
}
add_shortcode('get-reading-time', 'get_estimated_reading_time');
To use this you'll need to add the id in the parameter.
Example: [get-reading-time my-id="396774"]
Replace 396774 with the ID of the post you want to retrieve the time of.
Perfect it's ok
You're welcome Alexis!
Hi, this is a very good cue in my opinion.
I am trying to insert a "read time" to my posts and I too noticed that Yoast already calculates it automatically.
<meta name="twitter:data2" content="1 minuto" class="yoast-seo-meta-tag" />
So why put code in functions.php and create shortcodes if you can directly take the data created by Yoast?
Is there any way to do this using for example a Headline block of GenerateBlocks, set Dynamic Options and Post meta as Dynamic text type?
I hope I have explained myself.
Thanks in advance for the help :)
Hi there,
looking at the Yoast documentation - they have their own block to display it:
https://yoast.com/help/estimated-reading-time-in-yoast-seo/
Or alternatively if you want to use the a Headline block, then you would need a shortcode for that.
Elvin provided one here:
https://generatepress.com/forums/topic/creating-a-shortcode-with-yoast/#post-1891293
Thank you VERY MUCH David ( and Elvin :) )
You're welcome