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

773 ℃

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

进入前端入门建站教程官网入口

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

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

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

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

php语法如何实现自动替换超链接

标签: preg_replace, wordpress

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

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

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

生活小工具

收录了万年历、老黄历、八字智能排盘等100+款小工具!生活小工具
php语法中如何获取数组元素个数的2个方法!
利用css3做一个滚动卡片效果
JS在网站上显示页面加载时间
mysql获取指定表的所有字段名
织梦cms后台用什么SQL命令行工具替换或清除指定