Hexo根据更新时间修改文章排序机制

  改一行代码就行了。

前言(可不看)

之前由于电脑不在身边,长期通过其它电脑写文章利用之前说过的github action推送博客更新。但是发现一个问题,它总是按照文章创建时间排序,而不是文章更新时间。
查过网上很多方法,法一:博客源目录配置文件新增updated_option: mtime,不行。
法二:利用插件,
新增

1
2
3
index_generator:
    per_page: 10
    order_by: updated

不行。
直到最近拿到了电脑,终于有了源文件可以修改了。

解决方法

hexo源目录下,\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
'use strict';

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

module.exports = function(locals){
  var config = this.config;
  var posts = locals.posts.sort('-updated'); //修改这里! 原代码为var posts = locals.posts.sort('-date');
  var paginationDir = config.pagination_dir || 'page';

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

就这么简单。

Licensed under CC BY-NC-SA 4.0
最后更新于 0001-01-01 00:00 UTC
Comments
  • Latest
  • Oldest
  • Hottest
Powered by Waline v2.15.8
使用 Hugo 构建
主题 StackJimmy 设计