WordPress文章统计阅读时间估算

WordPress文章统计阅读时间估算

/**
* Gets the total word count and expected time to read the article
* @return type array
*/
function wp_reading_time_and_word_count() {
global $post;
// Predefined words-per-minute rate.
$words_per_minute = 225;
$words_per_second = $words_per_minute / 60;
// Count the words in the content.
$word_count = 0;
$text = trim( strip_tags( @get_the_content() ) );
if(!$text && is_object($post)){
$text = $post->post_content;
}
$word_count = substr_count( "$text ", ' ' );
// How many seconds (total)?
$seconds = floor( $word_count / $words_per_second );
$timereq = '';
if($seconds > 60){
$minutes = floor($seconds/60);
$seconds_left = $seconds % 60;
$timereq = 'PT'.$minutes.'M'.$seconds_left.'S';
}else{
$timereq = 'PT'.$seconds.'S';
}
return array('word_count' => esc_attr($word_count), 'timerequired' => esc_attr($timereq));
}

Leave a Reply

Your email address will not be published. Required fields are marked *