web建站教程
  1. 首页
  2. vuejs
  3. js
  4. 地图大全
  5. AIGC工具
  6. 前端知识
  7. 百度echarts
  8. 更多
    php入门
    nodejs
    mockjs
    reactjs
    mysql
    wordpress
    织梦cms
    帝国cms
    git教程
    IT知识
    模板大全
    休息站
    手机应用

wordpress如何获取文章字数和计算阅读时间

767 ℃

wordpress如何获取文章字数阅读时间,下面web建站小编给大家详细介绍一下如何实现代码!

找到当前主题函数模板functions.php中添加以下代码:

1、获取文章字数

// 字数统计
function get_words_count($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;
  }
}

模板中调用文章字数

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

2、获取文章阅读时间

// 阅读时间
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 get_reading_time() {
  echo zm_get_reading_time(get_the_content());
}

模板中调用文章阅读时间

<?php get_reading_time(); ?>

3、文章字数和文章阅读时间一起显示

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(); ?>

利用php语法做一个限流访问(排除指定IP)

wordpress建站,不要第三方插件实现图片自动添加水印(亲测有效)

wordpress建站,如何实现多个网站调用的文章图片来自同一个文件夹

wordpress在“页面”栏目找不到自己新建的“模板”

WordPress报错提示“正在执行例行维护,请一分钟后回来”解决方法?

标签: 文章字数 阅读时间

上面是“wordpress如何获取文章字数和计算阅读时间”的全面内容,想了解更多关于 wordpress 内容,请继续关注web建站教程。

当前网址:https://ipkd.cn/webs_3330.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

当前位置: 网站首页 > wordpress
本文共计1361个字,预计阅读时长10分钟
Trae:新一代免费的AI编程工具

生活小工具

收录了万年历、老黄历、八字智能排盘等100+款小工具!生活小工具
上一篇: 推荐一款由字制区设计团队的免费综艺字体——字制区喜脉体
下一篇: 推荐一款免费开源的跨平台MarkDown编辑器——Mark Text
x 打工人ai神器