Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

能添加置顶文章功能吗 #12

Open
HuRuWo opened this issue Feb 18, 2017 · 4 comments
Open

能添加置顶文章功能吗 #12

HuRuWo opened this issue Feb 18, 2017 · 4 comments

Comments

@HuRuWo
Copy link

HuRuWo commented Feb 18, 2017

**相关章节链接: **

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

@Allkoman
Copy link

Allkoman commented May 6, 2017

just add top : number in your title

@Durant35
Copy link

image

Test Results

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

@Durant35
Copy link

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

@stevenjoezhang
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants