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

推荐订阅源

小众软件
小众软件
V
Visual Studio Blog
博客园 - 三生石上(FineUI控件)
Last Week in AI
Last Week in AI
Blog — PlanetScale
Blog — PlanetScale
爱范儿
爱范儿
J
Java Code Geeks
A
About on SuperTechFans
F
Fortinet All Blogs
B
Blog
aimingoo的专栏
aimingoo的专栏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Engineering at Meta
Engineering at Meta
Y
Y Combinator Blog
有赞技术团队
有赞技术团队
G
Google Developers Blog
Apple Machine Learning Research
Apple Machine Learning Research
V
V2EX
博客园_首页
博客园 - 叶小钗
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
云风的 BLOG
云风的 BLOG

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-10-09 · via Allen Hua 的网络博客
阅读量:2249

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

要想实现标题中的功能,sql 语句可以这样写

select views, cid, from_unixtime(created, '%Y-%m-%d %H:%m:%s') as created_time, title, commentsNum from typecho_contents where type='post' and status='publish' order by views desc;

现在通过在 typecho 博客上实现,并在 归档页面 展示。下面展示我的方法。

functions.php 中增加以下函数

/**
 * 根据文章阅读数排序的热门发布文章
 */
function mostViewsPosts()
{
    $archive = Typecho_Widget::widget('Widget_Archive');
    // $hotNums = 5; //热门文章数
    // $minViews = 10; //最低阅读量
    $db = Typecho_Db::get();
    $select = $db->select()->from('table.contents')
        ->where('table.contents.type = ?', 'post')
        ->where('table.contents.status = ?', 'publish');
    //->limit($hotNums);
    $select->order("table.contents.views", Typecho_Db::SORT_DESC);
    $rows = $db->fetchAll($select);
    foreach ($rows as $row) {
        $mostViewsPosts[] = $archive->filter($row);
    }
    return $mostViewsPosts;
}

page-archives.php 中增加如下代码(用于 html 显示)

<hr />
    <article class="post">
        <?php $HotPosts = mostViewsPosts(); ?>
        <?php if (count($HotPosts) > 1) : ?>
            <div class="mostViewsPosts">
                <ul>
                    <h1 class="post-title">按照阅读数降序排序的热门文章</h1>
                    <?php $loopItem = 1; ?>
                    <?php foreach ($HotPosts as $v) { ?>
                        <a class="title" title="<?= $v['title']; ?>" href="<?= $v['permalink']; ?>">
                            <li>
                                <div><?= $v['title']; ?></div>
                            </li>
                        </a>
                        <span class="seqNumber">seq is <?= $loopItem++; ?></span><span class="views"> | <?= $v['views']; ?> 次阅读</span><span class="postTime"> | 发布于 <?= date('Y-m-d H:m:s', $v['created']); ?></span><span class="commentsNum"> | 已有 <?= $v['commentsNum']; ?> 条评论</span>
                        <br />
                        <br />
                    <?php } ?>
                </ul>
            </div>
        <?php endif; ?>
    </article>

tyle.min.css 中增加样式

.mostViewsPosts .title {
    color: black;
}

.mostViewsPosts .seqNumber {
    color: #FF9900;
}

.mostViewsPosts .views {
    color: red;
}

.mostViewsPosts .postTime {
    color: green;
}

.mostViewsPosts .commentsNum {
    color: blue;
}

a {
    color: black;
    text-decoration: none;
}

最终界面预览如下 归档页面

20211009200210.png

相关链接