WORDPRESS给文章添加预估阅读时间

浅唱丶
之前有写过通过the_content
挂钩实现预估阅读时间的文章
下面推荐一个更简单的方法实现阅读时间与文章字数,可以在任意位置进行调用,实现提高用户体验。
//定义字数和时间计算 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字/分的阅读速度进行计算,当然也可以自己修改。
扫一扫关注我们