WordPress教程 · WPtech

WordPress添加文章字数统计和预计阅读时间

小编 · 10月11日 · 2019年 ·

曾经在某些博客文章中看到有该篇文章的字数统计和预计阅读时间,这两个小数据还是比较有意思的,可以用读者用户在阅读前就大概知道这篇文章的长度及预计花费的时间.

WordPress添加文章字数统计和预计阅读时间

修改functions.php文件

将以下代码加到主题的 functions.php 文件最后一个 ?> 的前面。

[erphpdown]
//字数和预计阅读时间统计
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/400);
   $output .= '本文大约' . $text_num . '个字,预计阅读时间需要' . $read_time  . '分钟。';
   return $output;
}

[/erphpdown]

调用

再把调用统计代码添加到 对应的文章模板合适位置即可。

<?php echo count_words_read_time(); ?>

有疑问 , 请留言

0 条回应

必须 注册 为本站用户, 登录 后才可以发表评论!