Last active
July 18, 2022 11:06
-
-
Save webgurus/8b7dff77a9f6a3638e7a266ea92e149e to your computer and use it in GitHub Desktop.
Estimated post reading time (App.php) for Sage 9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Estimated Reading Time | |
* | |
* Use by adding {{ App::estimated_reading_time(get_the_ID()) }} to the Blade view where you want to use it. | |
* | |
* @return void | |
*/ | |
public static function estimated_reading_time($post_id) { | |
$the_content = apply_filters( 'the_content', get_the_content(null, false, $post_id)); | |
// count the number of words | |
$words = str_word_count( strip_tags( $the_content ) ); | |
// rounding off and deviding per 200 words per minute | |
$minute = floor( $words / 200 ); | |
// rounding off to get the seconds | |
// $second = floor( $words % 200 / ( 200 / 60 ) ); | |
// calculate the amount of time needed to read | |
$estimate = $minute . ' ' . __('min read','sage'); | |
// create output | |
// return the estimate | |
return $estimate; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment