[Resolved] Add reading time code

Home Forums Support [Resolved] Add reading time code

Home Forums Support Add reading time code

Viewing 8 posts - 16 through 23 (of 23 total)
  • Author
    Posts
  • #2361076
    alexis

    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

    Alexis
    agency K-LYA

    #2361102
    Fernando
    Customer Support

    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.

    #2361248
    alexis

    Perfect it’s ok

    Alexis
    agency K-LYA

    #2362068
    Fernando
    Customer Support

    You’re welcome Alexis!

    #2391146
    Jacopo

    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 ๐Ÿ™‚

    #2391293
    David
    Staff
    Customer Support

    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

    #2391491
    Jacopo

    Thank you VERY MUCH David ( and Elvin ๐Ÿ™‚ )

    #2391841
    David
    Staff
    Customer Support
Viewing 8 posts - 16 through 23 (of 23 total)
  • You must be logged in to reply to this topic.