自由屋推书网—热门的小说推荐平台!

你的位置: 首页 > wordpress分享

WordPress如何给主题文章页面添加文章字数和阅读时间

2022-01-04 11:58:44

我们是不是看到有些朋友的WordPress网站文章页面有当前文章的字数统计,以及预估的阅读时间。虽然这个功能没有多大用途,但是还是可以提高一些功能体验的。那这个功能如何加入呢?在这里也将这个方法整理看以后有需要的时候也加上。

第一、统计文章字数

// 字数统计 By itbulu.com
    function cnwper_count_words ($text) {
    	global $post;
    	if ( '' == $text ) {
    		$text = $post->post_content;
    		if (mb_strlen($output, 'UTF-8') < mb_strlen($text, 'UTF-8')) $output .= '<span class="word-count">共' . mb_strlen(preg_replace('/\s/','',html_entity_decode(strip_tags($post->post_content))),'UTF-8') .'字</span>';
    		return $output;
    	}
    }

加入到我们当前主题functions.php页面,然后在需要调用的时候直接用下面代码调用。

<?php echo cnwper_count_words($text); ?>

第二、预估阅读时间

// 统计预估阅读时间 By itbulu.com
    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(); ?>

如果有需要特定样式的话自己单独设置。

编辑推荐

热门小说