给WordPress添加文章字数和阅读时间

本文共计 474 个字,预计阅读时长 2 分钟

有一些wordpress网站博主想要在网站文章中添加一个预计阅读时间的功能,其实很简单,通过获取文章总文字数量除以预计每分钟阅读文字量就好了,今天分享一下实现方法。

示例

将下面的代码添加在当前使用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/300); // 修改数字300调整时间 $output .= '本文共计' . $text_num . '个字,预计阅读时长' . $read_time . '分钟。'; return $output; }

调用代码:

<?php echo count_words_read_time(); ?>

把上面的调用代码放至你当前使用WordPress主题的single.php中的合适的位置即可。