为WordPress文章详情页添加文章字数和阅读时间统计效果
我们经常看到一些wordpress博客网站文章详情页顶部有文章字数和阅读时间统计效果。现在这个社会的人都比较浮躁,有这个功能让用户知道大概几分钟能阅读完,起到了提示用户的作用,因为是源码对网站速度并没有影响,如果是插件就不建议安装了,虽然这个功能很多人认为作用不大,但是觉得千人千面嘛,根据自己需求来添加功能,还是可以提高一些功能上体验的。
那这个功能如何添加呢?在这里龙腾博客也将这个方法整理出来,以便有需求的朋友可以找到并添加此功能。
统计文章字数
// 字数统计 By blog.longtengwx.com
function cnwper_count_words ($text) {
global $post;
if ( '' == $text ) {
$text = $post->post_content;
if (mb_strlen($output, 'UTF-8') < mb_strlen($text, 'UTF-8')) $output .= '<span class="word-count">共' . mb_strlen(preg_replace('/\s/','',html_entity_decode(strip_tags($post->post_content))),'UTF-8') .'字</span>';
return $output;
}
}
添加到我们当前主题或子主题functions.php文件。
然后在single.php文件需要显示的位置调用下面简码。
<?php echo cnwper_count_words($text); ?>
预估阅读时间
// 统计预估阅读时间 By blog.longtengwx.com
function count_words_read_time () {
global $post;
$text_num = mb_strlen(preg_replace('/\s/','',html_entity_decode(strip_tags($post->post_content))),'UTF-8');
$read_time = ceil($text_num/300); // 修改数字300调整时间
$output .= '本文共计' . $text_num . '个字,预计阅读时长' . $read_time . '分钟。';
return $output;
}
添加到我们当前主题或子主题functions.php文件。
然后在single.php文件需要显示的位置调用下面简码。
<?php echo count_words_read_time(); ?>
如果有需要特定样式的话自己单独设置。













