Allen Hua's Another Blog
Life is going on, I do what I want.
Home
Categories
Archives
Tags
About
Home
Typecho 归档页面显示当前作者/用户的公开文章总数量
Typecho 归档页面显示当前作者/用户的公开文章总数量
取消
Typecho 归档页面显示当前作者/用户的公开文章总数量
由
dk11
发布于 2021-09-26
·
最后更新:2021-09-26
56
# 搜索已有解决方案 想要在我的 [归档](https://hellodk.cn/archives) 页面显示我发布的总的文章数量。简单搜了一下有下面四篇文章 - https://zezeshe.com/archives/typecho-author-postsnum.html - https://zezeshe.com/archives/typecho-author-allpostsnum.html - https://liudm.cn/04.html - https://www.dpaoz.com/57 看了之后不免怀疑,好像是真的抄来抄去的…… # 数据库验证 我这个没写过 php 的今天来试试实现今天这个需求。首先 ssh 进服务器,登录到 mysql 终端页面查看一下规律。 表 `typecho_contents` 的 `status` 字段有两种取值,分别是 `publish` 代表公开文章,`private` 私有文章。 ``` mysql> select count(distinct(status)) from typecho_contents; +-------------------------+ | count(distinct(status)) | +-------------------------+ | 2 | +-------------------------+ 1 row in set (0.00 sec) ``` `type` 字段取值有 `post` 和 `page`,post 是正常发布文章,page 是独立页面。 ``` mysql> select count(cid) from typecho_contents where type='post'; +------------+ | count(cid) | +------------+ | 167 | +------------+ 1 row in set (0.00 sec) ``` 过滤一下 `status` 是 publish 的,刚刚好就是我们需要的值。 ``` mysql> select count(cid) from typecho_contents where status='publish' and type='post'; +------------+ | count(cid) | +------------+ | 161 | +------------+ 1 row in set (0.00 sec) ``` 过滤一下 `status` 是 private 的,161+6=167,验证正确。 ``` mysql> select count(cid) from typecho_contents where status='private' and type='post'; +------------+ | count(cid) | +------------+ | 6 | +------------+ 1 row in set (0.00 sec) ``` # 前端验证 打开 [归档](https://hellodk.cn/archives) 页面,按下 F12 打开浏览器 Inspec 审查功能 ``` var archives = document.getElementById('archives'); var archivesLength = archives.getElementsByTagName('li'); console.log(archivesLength.length); ``` 最终 `console.log` 输出了 161,和我们从数据库中查到的一致。  # 写 PHP 函数 下面开始编写 php 代码。 可以参考 [上面引用的链接第二个](https://zezeshe.com/archives/typecho-author-allpostsnum.html) 他的代码,不过他的代码最终过滤出来的文章总数是公开的私有的总的文章数量(没有过滤 `status` 为 publish 的)。 我的代码最终如下,将以下代码添加进主题对应的 `functions.php` 中 ``` /** * 获取当前用户发布的权限是公开的文章总数量 */ function allpostnum($id) { $db = Typecho_Db::get(); $postnum = $db->fetchRow($db->select(array('COUNT(authorId)' => 'allpostnum'))->from('table.contents')->where('table.contents.authorId=?', $id)->where('table.contents.type=?', 'post')->where('table.contents.status=?', 'publish')); $postnum = $postnum['allpostnum']; return $postnum; } ``` # 前端调用并显示 在主题对应的 `page-archives.php` 文件适当的位置添加如下代码,我是放在 `<h1 class="post-title">` 和 `$output = '<div id="archives">';` 之间的。 ``` <h2 class="post-title"><?php $postNum=allpostnum($this->author->uid); echo "目前共计 {$postNum} 篇文章。继续努力。" ?> </h2> ``` 最终效果预览  end.
study
Typecho
PHP
MySQL
该博客文章由作者通过
CC BY 4.0
进行授权。
分享
最近更新
打卡南京市区人防工程纳凉点
Linux 利用 openssl 生成随机密码
收集、推荐一些白噪声网站
华强买瓜和高级特工穿山甲原版台词,要求全文背诵
OpenWrt 安装 shadow-gpasswd 从组中删除用户
热门标签
Java
笔记
随笔
日记
网络
Telegram
docker
GitHub
n1
随想
文章目录
Typecho 博客归档页面显示每年各发布了多少篇文章
小项目——保存 GitHub/V2EX/gravatar identicon “几何图案”风格的头像到本地文件夹