美文网首页
Hexo添加置顶功能

Hexo添加置顶功能

作者: Eazow | 来源:发表于2017-09-20 15:01 被阅读276次

1. 修改node_modules/hexo-generator-index/lib/generator.js

'use strict';
var pagination = require('hexo-pagination');
module.exports = function(locals){
  var config = this.config;
  var posts = locals.posts;
    posts.data = posts.data.sort(function(a, b) {
        if(a.top && b.top) { // 两篇文章top都有定义
            if(a.top == b.top) return b.date - a.date; // 若top值一样则按照文章日期降序排
            else return b.top - a.top; // 否则按照top值降序排
        }
        else if(a.top && !b.top) { // 以下是只有一篇文章top有定义,那么将有top的排在前面(这里用异或操作居然不行233)
            return -1;
        }
        else if(!a.top && b.top) {
            return 1;
        }
        else return b.date - a.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
    }
  });
};

2. 设置文章置顶

在文章Front-matter中添加top值,数值越大文章越靠前,如:

---
title: Hexo+nexT主题配置备忘
date: 2016-12-14 11:49:33
tags: [Hexo,next-theme,Seo]
categories: 学习笔记
top: 10
---

参考

http://blog.ynxiu.com/2016/hexo-next-theme-optimize.html

相关文章

  • Hexo添加置顶功能

    1. 修改node_modules/hexo-generator-index/lib/generator.js 2...

  • Hexo博客添加文章置顶功能

    原来用的WordPress,直接很方便地管理置顶文章,Hexo只提供了按发布日期的排序,只好网上找了些资料修改。 ...

  • hexo实现文章置顶功能

    想将写好的一些文章置顶,但是hexo好像默认不提供这个功能,希望以后的版本会有。这里简单写一下实现hexo文章置顶...

  • Hexo + next 主题添加搜索与分享功能

    Hexo + next 主题添加搜索与分享功能: 传送门

  • 新增Hexo博客文章置顶功能

    博主博客地址 修改Hexo文件夹下的node_modules/hexo-generator-index/lib/g...

  • hexo添加评论功能

    目前博客站点使用的评论功能,多说,网易云跟贴都已经下线。Disqus也被挡在墙外,友言貌似也不行。 可用的评论系统...

  • hexo添加评论功能

    多说和网易云已经没了,畅言需要备案,Disqus,Hypercomments和LiveRe都是国外的,加载速度贼慢...

  • 给博客添加文章目录

    前言 因为JSimple主题没有自带目录,所以需要自己动手给hexo博客文章添加目录功能。 第一步 查阅hexo文...

  • Hexo-Next-主题优化(三)

    1.博文置顶 打开Hexo 站点下node_modules/hexo-generator-index/lib/ge...

  • hexo使用小记

    添加域名绑定功能 直接在github库中添加CNAME文件,发现每次执行hexo d之后,CNAME文件就被覆盖了...

网友评论

      本文标题:Hexo添加置顶功能

      本文链接:https://www.haomeiwen.com/subject/kdrosxtx.html