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

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

345 ℃
     

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

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

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

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

php怎么删除字符串中指定字符

wordpress如何在搜索结果中排除指定分类的文章

标签: preg_replace, wordpress

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

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

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

当前位置: 网站首页 > wordpress
本文共计898个字,预计阅读时长6分钟
生活小工具,收录了80多款小工具
上一篇: 基于JavaScrip的web动画库——GSAP
下一篇: 免费商用中文字体——优设鲨鱼菲特健康体
x 打工人ai神器