WordPress 获取文章估算阅读时间
wordpress吧
全部回复
仅看楼主
level 14
原文来自Fatesinger.com,感谢分享。计算好文章字数,然后除以平均阅读速度,就得到估算阅读时间了。下面代码是2个函数,放到functions.php中即可,第一个函数是获取文章字数,第二个函数是计算阅读时间,300是设定的阅读速度。
functions:
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;
}
调用方式
<?phpechoest_read_time();?>
2015年05月03日 14点05分 1
level 13
这个有趣…[真棒]
   ——
gggak47.org
   一个专注于前端开发的博客
2015年05月03日 22点05分 2
level 14
这个方法太落后了,应该用js获取屏幕看到网页位置来计时,你这个只能实现普通计时,要是打开网站不读了呢,这个能用解决前端的为什么非要在后端加一堆函数呢。
2015年05月03日 23点05分 3
这个是计算阅读了多久的方法。
2015年05月03日 23点05分
至于估计阅读时间,为什么不交给js来做呢。
2015年05月03日 23点05分
[滑稽]才想到,js能找到一个div位置的高度不…不太懂这个
2015年05月04日 05点05分
@PC丶爱好者 当然可以,要不然你拉倒网站底部自动刷新的功能怎么弄的→_→
2015年05月04日 06点05分
level 10
3楼说的有道理,能交给前端做的尽量用前端做,粗略估计不需要统计的话JavaScript完全可以胜任。
2015年05月04日 01点05分 4
1