WordPress教程 —— 添加文章统计字数及大概阅读时间

1、将以下代码加到主题的 functions.php 文件最后一个 ?> 的前面。若?>省略了,则添加到最后就可以。

//字数和预计阅读时间统计
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;
}

其中,以上的 400 为阅读速度,可以更改。如只需要输出阅读时间或文章字数,只需要修改删除第 6 行某些代码即可,具体请自行 DIY 吧。

2、再把调用统计代码添加到主题下的single.php 文件适合的位置即可。

<?php echo count_words_read_time(); ?>

转发自: https://boke112.com/3845.html

© 版权声明
THE END
喜欢就支持一下吧
点赞0赞赏 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容