今天登录社区的时候看到有之前的文章有个留言的评论,说如何统计typecho所有文章的字数,这里分享一下代码。
在当前主题的functions.php文件中添加函数
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//字数统计function allOfCharacters() { $chars = 0; $db = Typecho_Db::get(); $select = $db ->select('text')->from('table.contents'); $rows = $db->fetchAll($select); foreach ($rows as $row) { $chars += mb_strlen(trim($row['text']), 'UTF-8'); } $unit = ''; if($chars >= 10000) { $chars /= 10000; $unit = 'w'; } else if($chars >= 1000) { $chars /= 1000; $unit = 'k'; } $out = sprintf('%.2lf %s',$chars, $unit); return $out;} |
joe主题
这里我用的joe主题,其他的主题也是一样
![图片[1]-typecho统计博客所有文章的字数实例详解-爱站](https://img.jbzj.com/file_images/article/202301/20230131090000026.png)
在底部页面模板
(通常是footer.php)添加调用代码
|
1
2
3
|
<div class="item " style="margin:0 0 0 10px"><span> 全站字数 <?php echo allOfCharacters(); ?>字</span> </div> |
全站字数
全站字数就是调用成功了,其他的typecho主题也是一样的方法。
原文链接:https://cloud.tencent.com/developer/article/2206519?areaSource=101001.31&traceId=MmlM48waNbYtozvF8jf1t
![图片[2]-typecho统计博客所有文章的字数实例详解-爱站](https://img.jbzj.com/file_images/article/202301/20230131090000027.png)
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END














暂无评论内容