- This topic has 33 replies, 5 voices, and was last updated 5 years, 4 months ago by Leo.
-
AuthorPosts
-
August 9, 2018 at 10:15 pm #644631Zad
Is there any way to create an estimated reading time for blog posts?
There two links seem to provide some code to do so and I was wondering if it was implementable on GeneratePress.
https://birchtree.me/blog/reading-time-wp-php/
https://www.binarymoon.co.uk/2013/10/wordpress-estimated-reading-time/
August 10, 2018 at 7:39 am #644869TomLead DeveloperLead DeveloperThose should both work just fine with GeneratePress. Where do you want it to show up?
August 10, 2018 at 12:15 pm #645232ZadHi,
I want to make it similar to stuff that I often see from Medium.com or the Hugo framework. Reading time would be next to date etc.
August 10, 2018 at 4:29 pm #645367TomLead DeveloperLead DeveloperSo first you’d add this function to your site:
function tu_estimated_reading_time() { $post = get_post(); $words = str_word_count( strip_tags( $post->post_content ) ); $minutes = floor( $words / 120 ); $seconds = floor( $words % 120 / ( 120 / 60 ) ); if ( $minutes < 2 ) { $estimated_time = $minutes . ' minute' . ($minutes == 1 ? '' : 's') . ', ' . $seconds . ' second' . ($seconds == 1 ? '' : 's'); } else { $estimated_time = $seconds . ' second' . ($seconds == 1 ? '' : 's'); } return $estimated_time; }
Then you’d do this:
add_filter( 'generate_post_date_output', function( $output ) { return $output . ' ' . tu_estimated_reading_time(); } );
Let me know if that works or not 🙂
August 10, 2018 at 4:31 pm #645371ZadThanks! Where would I add these codes? To the function.php file? Code snippets? Or hooks?
August 10, 2018 at 6:26 pm #645408LeoStaffCustomer SupportOne of these methods here:
https://docs.generatepress.com/article/adding-php/August 10, 2018 at 7:31 pm #645433ZadHi, when I try to insert the code I get an error for this line
if ( 1 < = $minutes ) {
Apparently the = sign is unexpected
August 10, 2018 at 8:34 pm #645460TomLead DeveloperLead DeveloperI just made an adjustment to the code above – can you check again?
August 10, 2018 at 9:40 pm #645480ZadSeems to put it in a weird place and also estimate the time in a weird way
I generally want the time below the author and date. My date is also the last updated date rather than the original post date, added CSS from the Documentation page for that
August 11, 2018 at 9:06 am #645787TomLead DeveloperLead DeveloperYou can try this for your second function instead:
add_filter( 'generate_post_author_output', function( $output ) { $output .= '<div class="read-time">Reading time: ' . tu_estimated_reading_time() . '</div>'; return $output; } );
August 11, 2018 at 11:59 am #645873ZadIt looks good now but for some reason the function isn’t calculating the times correctly
Its estimating it to be abnormally low
August 11, 2018 at 8:07 pm #646048TomLead DeveloperLead DeveloperThat’s just one of the functions you found.
We could try simplifying it..
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 ); $time = ceil( $word_count / $wpm ); return $time; }
August 11, 2018 at 8:27 pm #646053ZadDoesn’t seem to work. Maybe the function itself is fine but I think the add filter may need to be updated?
I have a gravatar entry meta and css code for last Updated.
August 11, 2018 at 8:50 pm #646059TomLead DeveloperLead DeveloperDid you keep this filter added?: https://generatepress.com/forums/topic/creating-an-estimated-reading-time/#post-645787
Your full functions should look like this:
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 ); $time = ceil( $word_count / $wpm ); return $time; } add_filter( 'generate_post_author_output', function( $output ) { $output .= '<div class="read-time">Reading time: ' . tu_estimated_reading_time() . '</div>'; return $output; } );
August 11, 2018 at 8:53 pm #646062ZadYup, doesn’t seem to work
-
AuthorPosts
- The topic ‘Creating an Estimated Reading Time’ is closed to new replies.