之前在WordPress中加入function.php文章估计阅读时间代码是用的bigfa的,发现并不准确,2727字估算约19分钟,实际阅读完5分钟左右。网上又找到一段代码,才符合实际。略有修改
// 字数统计
function count_words ($text) {
global $post;
if ( '' == $text ) {
$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;
}
}
引用代码<?php echo count_words(); ?>
//阅读时间,正常每分钟500字
function read_time( $return = false) {
$wordcount = round(count_words(), -2);
$minutes = ceil($wordcount / 500);if ($wordcount <= 500) {
$output = '阅读时间约1分钟';
} else {
$output = '阅读时间约'.$minutes.'分钟';
}
echo $output;
}
引用代码<?php echo read_time(); ?>
很有趣的功能~