惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
宝玉的分享
宝玉的分享
月光博客
月光博客
B
Blog
云风的 BLOG
云风的 BLOG
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
aimingoo的专栏
aimingoo的专栏
N
Netflix TechBlog - Medium
博客园_首页
GbyAI
GbyAI
人人都是产品经理
人人都是产品经理
A
About on SuperTechFans
Y
Y Combinator Blog
L
LangChain Blog
有赞技术团队
有赞技术团队
D
Docker
爱范儿
爱范儿
博客园 - 司徒正美
H
Hackread – Cybersecurity News, Data Breaches, AI and More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
酷 壳 – CoolShell
酷 壳 – CoolShell
Microsoft Security Blog
Microsoft Security Blog
D
DataBreaches.Net

Allen Hua 的网络博客

由于 Linux 桌面没有一个好用的天气程序就写了一个跨平台的CheckitoutWeather 给 caesium-image-compressor 图片压缩程序构建了 Linux AppImage v2.8.5 最新版 debian13(debian trixie)安装了nvidia闭源驱动后从x11切换到wayland的方法 山间摩旅追风,偶遇一场绚烂晚霞 最近使用debian系统的一些心得 机械革命无界14Pro笔记本debian forky成功驱动内置扬声器和麦克风 记录 typecho 1.2.0 升级到 1.3.0 过程 开发了一款openwrt插件:文本剪贴板 解决机械革命笔记本内屏高刷240Hz闪屏问题 使用 Java 写了一个局域网端口扫描器 - Allen Hua 的网络博客 openwrt使用外置根extroot机制扩展根分区大小 - Allen Hua 的网络博客 将机场ss节点批量转换成ss字符串链接批量添加到passwall - Allen Hua 的网络博客 给机械革命钛钽plus换屏:NY2换成NZ2 - Allen Hua 的网络博客 记录一次pve宿主机和上面的debian虚机无故down机事件 - Allen Hua 的网络博客 给图床部署cdn腾讯云的edgeone并排查Cache-Control max-age 3600的问题 - Allen Hua 的网络博客 从高山草甸到徽派古村:武功山反穿与皖浙赣自驾行记 - Allen Hua 的网络博客 增程器就是充电宝?别被忽悠了 - Allen Hua 的网络博客 浦口龙虎巷扫街,记录人间真实 博客图片压缩方案更新|AVIF|WebP|MozJPEG|标准JPEG Windows 电脑使用 Obs Studio 录制各个网站视频/桌面画面教程 为typecho博客添加latex支持 新能源汽车之纯电车使用交流慢充和直流快充的充电损耗对比 2025年3月更新全国5A景区名录 2025最新查看小米/红米手机电池健康度和循环次数方法 完美解决 seafile FILE_SERVER_ROOT 配置导致的内网外网不能同时访问和上传下载的问题 纯css实现typecho博客文章文字spoiler剧透效果 我对Typecho Facile主题的一些修改,图片懒加载优化,样式定制 2024年8月我的宜昌 - 重庆 - 川西小环线自驾旅行分享 2022年打卡南京市区人防工程纳凉点 2024年带着A7C2+腾龙28-200 再次来到红山森林动物园
Typecho 归档页面显示当前作者/用户的公开文章总数量
Allen Hua · 2021-09-26 · via Allen Hua 的网络博客
阅读量:4213

warning: 这篇文章距离上次修改已过1703天,其中的内容可能已经有所变动。

搜索已有解决方案

想要在我的 归档 页面显示我发布的总的文章数量。简单搜了一下有下面四篇文章

看了之后不免怀疑,好像是真的抄来抄去的……

数据库验证

我这个没写过 php 的今天来试试实现今天这个需求。首先 ssh 进服务器,登录到 mysql 终端页面查看一下规律。

typecho_contentsstatus 字段有两种取值,分别是 publish 代表公开文章,private 私有文章。

mysql> select count(distinct(status)) from typecho_contents;
+-------------------------+
| count(distinct(status)) |
+-------------------------+
|                       2 |
+-------------------------+
1 row in set (0.00 sec)

type 字段取值有 postpage,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)

前端验证

打开 归档 页面,按下 F12 打开浏览器 Inspec 审查功能

var archives = document.getElementById('archives');
var archivesLength = archives.getElementsByTagName('li');
console.log(archivesLength.length);

最终 console.log 输出了 161,和我们从数据库中查到的一致。

20210926211923.png

写 PHP 函数

下面开始编写 php 代码。

可以参考 上面引用的链接第二个 他的代码,不过他的代码最终过滤出来的文章总数是公开的私有的总的文章数量(没有过滤 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>

最终效果预览

20210926211739.png

end.