11. “分类”页面 文件夹排序(支持优先级自定义)
新功能:分类页面支持关键词优先级排序
功能简介 | Feature Introduction
在 Hexo Fluid 主题的分类页面,现在可以通过配置"优先级关键词表",让你关心的分类(如 Transformer、NLP、LLMs 等)优先显示,其余分类自动排在后面。这对于内容较多、分类较多的博客非常实用。
如何配置 | How to Configure
-
在主题配置文件中添加优先级关键词表
打开
themes/fluid/_config.yml,在category配置下添加:1
2
3
4category:
enable: true
# ... 其他配置 ...
priority_keywords: ["CNNs", "RNNs", "NLP", "Attention", "Transformer", "LLMs", "System", "Hexo", "Projects"]按你的需求调整关键词顺序和内容。
-
修改分类渲染模板
编辑
themes/fluid/layout/_partials/category-list.ejs,在render_categories函数内增加如下排序逻辑:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16<%
// 获取优先级关键词
var priorityKeywords = theme.category.priority_keywords || [];
// 对 curCats 进行排序
var sortedCats = curCats.data.slice().sort((a, b) => {
const aIdx = priorityKeywords.indexOf(a.name.trim());
const bIdx = priorityKeywords.indexOf(b.name.trim());
if (aIdx === -1 && bIdx === -1) return 0; // 都不在优先级表,保持原顺序
if (aIdx === -1) return 1; // a 不在,b 在,b 靠前
if (bIdx === -1) return -1; // b 不在,a 在,a 靠前
return aIdx - bIdx; // 都在,按表顺序
});
%>
<% return sortedCats.forEach((cat) => { %>
...
<% }) %> -
确保 theme 变量传递到 partial
在
themes/fluid/layout/categories.ejs调用 partial 时,加入theme:1
2
3
4
5
6
7
8
9<%- partial('_partials/category-list', {
curCats: curCats,
params: {
orderBy: orderBy,
postLimit : theme.category.post_limit,
postOrderBy: theme.category.post_order_by || config.index_generator.order_by
},
theme: theme // 传递 theme
}) %>
涉及文件 | Files Modified
themes/fluid/_config.ymlthemes/fluid/layout/_partials/category-list.ejsthemes/fluid/layout/categories.ejs
总结 | Summary
- 通过配置
priority_keywords,让重要分类优先展示。 - 适用于内容多、分类多的博客,提升用户体验。
- 只需简单配置和模板修改即可实现。
“觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”
微信支付
支付宝支付
11. “分类”页面 文件夹排序(支持优先级自定义)
http://neurowave.tech/2025/02/15/1-11-分类标签排序/