按照创建时间或更新时间排序

此部分配置需要在_config.yml文件中配置index_generator项

1
2
3
4
5
6
7
8
# Home page setting
# path: Root path for your blogs index page. (default = '')
# per_page: Posts displayed per page. (0 = disable pagination)
# order_by: Posts order. (Order by date descending by default)
index_generator:
path: ''
per_page: 9
order_by: -updated # -:倒序 updated:更新时间 date:创建时间(默认)

自定义排序

通过index_generator查找文件时发现了 ./node_modules/hexo-generator-index/lib/generator.js 文件.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
'use strict';

const pagination = require('hexo-pagination');

module.exports = function(locals) {
const config = this.config;
const posts = locals.posts.sort(config.index_generator.order_by);

posts.data.sort((a, b) => (b.sticky || 0) - (a.sticky || 0));

const paginationDir = config.pagination_dir || 'page';
const path = config.index_generator.path || '';

return pagination(path, posts, {
perPage: config.index_generator.per_page,
layout: ['index', 'archive'],
format: paginationDir + '/%d/',
data: {
__index: true
}
});
};

其中sort的内容除了config.index_generator.order_by 上面配置的 -updated 外,还有一个是post.data.sticky
所以在文章头部中配置sticky的值,文章将会sticky大小来进行自定义的排序。

1
2
3
4
5
---
title: Hexo文章排序和自定义排序
tags: [Hexo, 主题]
sticky: 100
---