怎么为WordPress文章添加字数和阅读时间

追格官方小助手/ 2022年02月24日/ WordPress/ 浏览 1413

我们经常在阅读某些文章的时候,都会提示本文xx字,大概需要几分钟,那么怎么为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/888); // 修改数字888调整时间
	$output .= '本文共计' . $text_num . '个字,预计阅读时长' . $read_time  . '分钟。';
	return $output;
}


2、然后在当前WordPress模板single.php中的合适位置添加以下代码


<?php echo count_words_read_time(); ?>


发表评论

暂无评论,抢个沙发...

客服 工单