无需下载插件,添加一段代码即可
修改Hexo/yourblog
文件夹下的node_modules/hexo-generator-index/lib/generator.js
需要添加的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13
| posts.data = posts.data.sort(function(a, b) { if(a.top && b.top) { if(a.top == b.top) return b.date - a.date; else return b.top - a.top; } else if(a.top && !b.top) { return -1; } else if(!a.top && b.top) { return 1; } else return b.date - a.date; });
|
以下是最终的generator.js内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| 'use strict';
const pagination = require('hexo-pagination'); const { sort } = require('timsort');
module.exports = function(locals) { const config = this.config; const posts = locals.posts.sort(config.index_generator.order_by);
posts.data = posts.data.sort(function(a, b) { if (a.top && b.top) { if (a.top == b.top) return b.date - a.date; else return b.top - a.top; } else if (a.top && !b.top) { return -1; } else if (!a.top && b.top) { return 1; } else return b.date - a.date; });
sort(posts.data, (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 } }); };
|
使用方法:在需要置顶的文章添加top属性即可,排序从小到大
![](/hexo%E5%8D%9A%E5%AE%A2%E6%96%87%E7%AB%A0%E7%BD%AE%E9%A1%B6%E5%8A%9F%E8%83%BD/hexo%E5%8D%9A%E5%AE%A2%E6%96%87%E7%AB%A0%E7%BD%AE%E9%A1%B6.png)
文章转自博客园:試毅-思伟