新增Hexo博客文章置顶功能
博主博客地址
修改Hexo文件夹下的node_modules/hexo-generator-index/lib/generator.js
需要添加的代码:
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内容:
'use strict'; var pagination = require('hexo-pagination'); module.exports = function(locals) { var config = this.config; var 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; }); var paginationDir = config.pagination_dir || 'page'; var 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属性即可,排序从小到大
博客效果:
Tips:常用hexo命令
# hexo n == hexo new # hexo g == 生成 # hexo s == 启动服务
相关推荐
JingLisen 2020-06-26
hzq0 2020-06-16
蓝亭书序 2020-06-04
Amzaing00 2020-05-11
hzq0 2020-05-10
乾坤一碼農 2020-05-10
gakki的二向箔 2020-05-07
hzq0 2020-05-05
hzq0 2020-05-05
只有代码的世界 2020-04-27
JingLisen 2020-04-23
gakki的二向箔 2020-04-07
89284553 2020-03-09
YULIU 2020-03-07
只有代码的世界 2020-03-04
hzq0 2020-02-21
只有代码的世界 2020-02-19
YULIU 2020-02-18