web建站教程
     
  1. 首页
  2. 前端UI组件库
  3. AI项目和框架
  4. 前端知识
  5. AI应用
  6. IT知识
  7. ComfyUI插件
  8. 地图大全
  9. 休息站

wordpress添加文章字数统计和预计阅读时间代码

955 ℃

wordpress添加文章字数统计代码

1、将以下代码加到主题的 functions.php 文件最后一个 ?> 的前面。

//字数统计
function count_words ($text) {
  global $post;
  if ( '' == $text ) {
    $text = $post->post_content;
    if (mb_strlen($output, 'UTF-8') < mb_strlen($text, 'UTF-8')) $output .= '本文共' . mb_strlen(preg_replace('/\s/','',html_entity_decode(strip_tags($post->post_content))),'UTF-8') . '个字';
    return $output;
  }
}

2、再把调用统计代码加到自己认为适合的位置,如 single.php 文件的某个位置。

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

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/400);
  $output .= '本文共' . $text_num . '个字,预计阅读时间需要' . $read_time  . '分钟。';
  return $output;
}

2、再把调用统计代码添加到 single.php 文件适合的位置即可。

<?php echo count_words_read_time(); ?>

ZipWP官网:一个AI驱动的零代码WordPress建站神器

小半WordPress AI助手:开源免费的AI插件,为WordPress网站注入智能内容创作能力

WordPress ai助手:为 WordPress 用户提供集成多种 AI 模型的功能

php语法如何替换数组中的字符串

wordpress利用mysql让文章 ID重新排列

标签: preg_replace wordpress

上面是“wordpress添加文章字数统计和预计阅读时间代码”的全面内容,想了解更多关于 wordpress 内容,请继续关注web建站教程。

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

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

当前位置: 首页 > wordpress
扣子空间 – 免费全能AI办公智能体
Trae:新一代免费的AI编程工具
上一篇:
下一篇:
x 打工人ai神器
x 扫一扫打开藏历小程序

打开微信“扫一扫”查看今日藏历!