您现在的位置是:首页 > 教程 > WordPress教程WordPress教程
WordPress主题怎么添加文章字数和需要阅读时间
如萱2023-07-03 22:21:50WordPress教程已有人查阅
导读显示文章字数和阅读时间,这功能有什么用呢,我也不知道,不过有用户说人家有我们能不能也加一个,那就加吧。
显示文章字数和阅读时间,这功能有什么用呢,我也不知道,不过有用户说人家有我们能不能也加一个,那就加吧。下面由WordPress教程栏目给大家介绍为WordPress主题添加文章字数和阅读时间的方法。
为WordPress主题添加文章字数和阅读时间为WordPress主题添加文章字数和阅读时间
具体代码这里也分享一下:
文章字数统计
// 字数统计
function zm_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 中。
文章阅读时间
// 阅读时间
function zm_get_reading_time($content) {
$zm_format = '<span class="reading-time">阅读时间%min%分%sec%秒</span>';
$zm_chars_per_minute = 300; // 估算1分种阅读字数
$zm_format = str_replace('%num%', $zm_chars_per_minute, $zm_format);
$words = mb_strlen(preg_replace('/s/','',html_entity_decode(strip_tags($content))),'UTF-8');
$minutes = floor($words / $zm_chars_per_minute);
$seconds = floor($words % $zm_chars_per_minute / ($zm_chars_per_minute / 60));
return str_replace('%sec%', $seconds, str_replace('%min%', $minutes, $zm_format));
}
function zm_reading_time() {
echo zm_get_reading_time(get_the_content());
}
代码添加到当前主题函数模板 functions.php 中。
调用文章字数和阅读时间代码
显示文章字数代码:
<?php echo zm_count_words($text); ?>
显示阅读时间代码:
<?php zm_reading_time(); ?>
将上述调用代码加到当前主题正文模板的适当位置即可。
不过字数统计和阅读时间不是很 ,特别是阅读时间,更是扯淡,默认是按 广播员语速定的。
写完这篇文章,发现网上有更简洁的代码,区别是上面的代码 到秒,下面的代码只估算到分。
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(); ?>
本文标签:
很赞哦! ()
相关文章
随机图文
-
wordpress主题模版怎么去除版权,wordpress主题版权怎么删除
1、打开主题文件夹:wp-content/themes/主题名称/footer.php;3、删除这段代码即可,删除后会什么都不显示,这样不是很美观,可己添加下自己的版权信息,可以替换为以下代码: -
wordpress翻译英文的插件有哪些
现而今,最通用的语言当属英语。Ethnologue(民族语言网)在其2015年版用户手册中指出,在全球74亿人中,有近10亿人以某种形式说英语,有4亿人以英语为母语。 -
WordPress可以配置163邮箱吗
WordPress可以配置163邮箱。下面我们来一下在wordpress博客上如何设置用163邮箱账号发送邮件。 -
wordpress上传本地视频的方法
上传本地视频到wordpress博客可以有两种方法,一是,后台写文章的时候直接上传插入视频文件上传,二是,通过ftp工具上传到服务器里面。
留言与评论 (共有 条评论) |