原文来自Fatesinger,感谢分享。计算好文章字数,然后除以平均阅读速度,就得到估算阅读时间了。下面代码是2个函数,放到functions.php中即可,第一个函数是获取文章字数,第二个函数是计算阅读时间,300是设定的阅读速度。
实现代码
function count_words () { global $post; $text = $post->post_content; if (mb_strlen($output, 'UTF-8') < mb_strlen($text, 'UTF-8')) $output .= mb_strlen(preg_replace('/\s/','',html_entity_decode(strip_tags($post->post_content))),'UTF-8'); return $output; } function read_time( $return = false) { $wordcount = round(count_words(), -2); $minutes = ceil($wordcount / 300); if ($wordcount <= 150) { $output = '预计阅读时间:1分钟'; } else { $output = '预计阅读时间:'.$minutes.'分钟'; } echo $output; }
调用方式
<?php echo est_read_time(); ?> 在文章循环中调用。