WP酷 WP酷
  • 主题
  • 教程
  • 插件
  • 官方 QQ 群
  • 建站推荐
  • 联系
  • nicetheme® 奈思主题
  • 注册
    登录
立即登录
  • 请到 [后台->外观->菜单] 中设置菜单
首页 › WordPress 教程 › WordPress 获取文章估算阅读时间

WordPress 获取文章估算阅读时间

PCDotFan9年前

原文来自Fatesinger,感谢分享。计算好文章字数,然后除以平均阅读速度,就得到估算阅读时间了。下面代码是2个函数,放到functions.php中即可,第一个函数是获取文章字数,第二个函数是计算阅读时间,300是设定的阅读速度。

 

实现代码

function count_words () {
    global $post;
    $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;

}

function read_time( $return = false) {
    $wordcount = round(count_words(), -2);
    $minutes = ceil($wordcount / 300);
    
    if ($wordcount <= 150) {
        $output = '预计阅读时间:1分钟';
    } else {
    $output = '预计阅读时间:'.$minutes.'分钟';
}
echo $output;
    
}

 

调用方式

<?php echo est_read_time(); ?> 在文章循环中调用。

#阅读时间
0
分享

本站下载管理系统「dl.mywpku.com」因年久失修而永久关闭。若需获得往年主题,请至 WP酷的百度网盘(提取码: 5rbm) 自行搜索下载。默认解压密码为「www.mywpku.com」。

对您造成的不便,敬请谅解!

PCDotFan 站长
文章 435评论 859
相关文章
  • [6.0 更新中文汉化]极度流行的SEO插件 – WordPress SEO by Yoast
  • WordPress 新用户注册邮件链接提示「您的密码重设链接无效」
  • AMP – 为 WordPress 移动站点操作全球访问优化
  • 去除谷歌服务 – WordPress 国外主题全方位加速
  • 使用 WP-CLI 简化 WordPress 操作流程
  • WordPress 基础开发 – 一探 WordPress Hook 机制及示例应用
  • 优化静态资源 – WordPress 国外主题全方位加速
  • WordPress 实现微信登录
评论 (1)
再想想
    PCDotFan
    站长
    若无特别注明,则默认我发布的所有文章均为内容原创 / 翻译原创,转载时请保留来源。
    435文章
    859评论
    3K获赞
    猜你喜欢
    WordPress 短代码制作教程
    10年前
    利用审查元素轻松删改WP主题
    10年前
    WP-Eloquent – 让 WordPress 像 Laravel 一样操作数据库
    6年前
    • 杂记
    • WordPress 付费主题推荐
    Copyright © 2023 WP酷. Designed by nicetheme. 琼ICP备13002067号-1