yelee icon indicating copy to clipboard operation
yelee copied to clipboard

能添加置顶文章功能吗

Open HuRuWo opened this issue 8 years ago • 4 comments

**相关章节链接: **

希望能够把某些文章置顶让所有人第一时间看到,要如何做呢

HuRuWo avatar Feb 18 '17 15:02 HuRuWo

just add top : number in your title

Allkoman avatar May 06 '17 22:05 Allkoman

image

Test Results

  • 添加 top: number 一行属性在.md文件开头的属性中可以实现置顶
  • number 值越大,越在列表前面
  • 除了按照top数值排、按照创建时间排,还可以按照更新时间排。 ps: @MOxFIVE 可以close了。

Durant35 avatar Aug 16 '17 01:08 Durant35

Refer: 解决Hexo置顶问题

  • 目前我自己的 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) {                        // 若top值一样
            if(config.index_generator.order_by == 'updated') {
              // 按照文章修改日期降序排
              return b.updated - a.updated;
            }
            else {
              // 按照文章日期降序排
              return b.date - a.date; 
            }
          }
          else return b.top - a.top;                  // 否则按照top值降序排
      }
      else if(a.top && !b.top) {                      // 以下是只有一篇文章top有定义,那么将有top的排在前面
          return -1;
      }
      else if(!a.top && b.top) {
          return 1;
      }
      else {                                          // 都没定义top值
        if(config.index_generator.order_by == 'updated') {
          // 按照文章修改日期降序排
          return b.updated - a.updated;
        }
        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
    }
  });
};
  • 可以通过站点下面的配置文件_config.yml进行配置
index_generator:
  path: ''
  per_page: 10 ##首頁默認8篇文章標題 如果值爲0不分頁
  # order_by: updated
  order_by: date

@HuRuWo

Durant35 avatar Aug 16 '17 02:08 Durant35

Hexo 已实现文章置顶参数: hexojs/hexo-generator-index#51

stevenjoezhang avatar Aug 12 '20 03:08 stevenjoezhang