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;
}
然后根据需要的位置添加调用
echo wpcount_words_read_time();
同样是根据400字/分的阅读速度进行计算,当然也可以自己修改。
本文由:小天丶 发布于 小菜的IT网,转载请注明出处:https://www.xiaocaiit.cn/461.html
评论此文章
已有0人参与了评论