- This topic has 33 replies, 5 voices, and was last updated 4 years, 2 months ago by
Leo.
-
AuthorPosts
-
August 9, 2018 at 10:15 pm #644631
Zad
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/
GeneratePress 2.1.3GP Premium 1.6.2August 10, 2018 at 7:39 am #644869Tom
Lead DeveloperLead DeveloperThose should both work just fine with GeneratePress. Where do you want it to show up?
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-developmentAugust 10, 2018 at 12:15 pm #645232Zad
Hi,
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 #645367Tom
Lead 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 🙂
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-developmentAugust 10, 2018 at 4:31 pm #645371Zad
Thanks! Where would I add these codes? To the function.php file? Code snippets? Or hooks?
August 10, 2018 at 6:26 pm #645408Leo
StaffCustomer SupportOne of these methods here:
https://docs.generatepress.com/article/adding-php/Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/August 10, 2018 at 7:31 pm #645433Zad
Hi, 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 #645460Tom
Lead DeveloperLead DeveloperI just made an adjustment to the code above – can you check again?
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-developmentAugust 10, 2018 at 9:40 pm #645480Zad
Seems 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 #645787Tom
Lead 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; } );
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-developmentAugust 11, 2018 at 11:59 am #645873Zad
It 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 #646048Tom
Lead 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; }
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-developmentAugust 11, 2018 at 8:27 pm #646053Zad
Doesn’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 #646059Tom
Lead 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; } );
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-developmentAugust 11, 2018 at 8:53 pm #646062Zad
Yup, doesn’t seem to work
-
AuthorPosts
- The topic ‘Creating an Estimated Reading Time’ is closed to new replies.