想要给自己搭建的WordPress网站添加文章字数统计和阅读时间的方法

  学习WordPress是很多建站新手都要面对的,今天小编为大家介绍一个WordPress小功能,就是在自己网站文章的底部添加文章字数统计和阅读时间的功能。想要给自己搭建的WordPress网站添加文章字数统计和阅读时间的方法是什么?

  这个功能可以统计一篇文章总共有多少字,并且预计文章阅读时间。下面来学习一下方法:

  1.打开自己的WordPress模板文件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;
}

  2.在网站的文章模板single.php里使用以下的标签来调用相关数据。

<?php echo count_words_read_time(); ?>

  3.这样Wordpress程序会自动统计文章字数和阅读时间了。