

Typecho 根据文章阅读数降序排序输出“热门文章”列表
source link: https://hellodk.cn/post/826
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

要想实现标题中的功能,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;
color: black;
text-decoration: none;
最终界面预览如下 归档页面
Recommend
-
31
大家可能对索引比较熟悉,而对降序索引比较陌生,事实上降序索引是索引的子集。我们通常使用下面的语句来创建一个索引:create index idx_t1_bcd on t1(b,c,d);上面sql的意思是在t1表中,针对b,c,d三个字段创建一个联合索引。但是大家不知道的是,上面这个sql实际...
-
29
什么是降序索引大家可能对索引比较熟悉,而对降序索引比较陌生,事实上降序索引是索引的子集。我们通常使用下面的语句来创建一个索引:create index idx_t1_bcd on t1(b,c,d);上面sql的意思是...
-
3
作者 | Deribit Market Research 翻译:子铭 如果未来还是央行的时代呢? Maker是一个数字货币领域的“中央银行”,其采取“货币政策”的方式与传统金融市场中央银行的方式相同。我们将通过分析Maker在面对Dai的微观通...
-
17
为 Typecho 文章页加入微信分享功能 2018.09.03默认分类 0 评...
-
6
写在前面的话 虽然已经有很多人总结过这十大排序算法,优秀的文章也不少,但是Java完整版的好像不多,还存在某些文章代码存在错误的情况,同时也为了自己练手,决定把所有的写一遍巩固下,同时也真诚的希望阅读到这篇文章的小伙伴们可...
-
10
Typecho根据文章cid与字段名获取字段值 2022-01-1110:35...
-
6
密码保护:Typecho 文章部分加密和显示标题 于 2021-03-16 由 jinlife 发布 t...
-
6
给 Typecho 文章设置 meta data 让链接预览时具有标题、简介以及缩略图 前言 在用 Slack、Mattermost、钉钉、飞书的时候,聊天中发出 Typecho 博客文章的链接之后...
-
6
WordPress免插件实现文章阅读数 2022-9-10 17:45:48 | 作者: 月光 | 分类:
-
10
用 AI 为 Typecho 博客文章生成摘要吧! 2023.12.03 |
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK