我们看到有些网友的WordPress站点文章页面是有在标题下面有给添加当前篇幅的字数以及阅读时间的,这个到底如何添加的呢?实际上这个加上去确实有时候还可以提高一些体验度。

//定义字数和时间计算
function wpcount_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;
}

然后根据需要的位置添加调用。
<?php echo wpcount_words_read_time(); ?>

这里是根据每分钟400字的阅读速度计算的,我们也可以根据其他频率计算。


扫描二维码,在手机上阅读!