邓先生的资源库

WordPress添加文章字数统计和预计阅读时间

一、为 WordPress 添加文章字数统计

1、将以下代码加到主题的 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;
}

其中,以上的 400 为阅读速度,可以更改。如只需要输出阅读时间或文章字数,只需要修改删除第 6 行某些代码即可,具体请自行 DIY 吧。

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

<?php echo count_words_read_time(); ?>

本原创文章未经允许不得转载 | 当前页面:邓先生的资源库 » WordPress添加文章字数统计和预计阅读时间

评论


|  Sitemap  |  App  |  Status  |  Stats  |  E-Mail |

已续  ,感谢有你
陕ICP备2022002237号

  开往-友链接力  飞翔