1. 卸载hexo-generator-index插件

hexo站点目录下,输入下面命令卸载默认的排序插件

npm uninstall hexo-generator-index --save

2. 安装hexo-generator-index-pin-top插件

hexo站点目录下,输入下面命令安装新的排序插件

npm install hexo-generator-index-pin-top --save

3. 添加front-matter配置项

在需要置顶的文章的Front-matter中加上top: 数值即可实现文章按照top值排序。比如下面这篇文章:

title: test
date: 2021-09-17 10:25:54
categories: 技术
top: 1

注意:

  • 经过以上三步,博客文章按照top值排序的功能按理说已经可以实现了,但是Sakura主题有一点不同。因为Sakura主题在themes\sakura\layout_partial\文件里面的archive.ejs 和 category-archive.ejs 中设置了按照日期排序,所以还需要修改archive.ejs 和 category-archive.ejs

4. 修改themes\Sakura\layout_partial文件里面的 archive.ejs 和 category-archive.ejs ( 仅Sakura主题需要进行第四步 )

原来的archive.ejs:

<% if (pagination == 2){ %>
  <!-- 首页默认取最最新文章集 -->
  <% page.posts.sort('date', theme.homePageSortType).limit(theme.homeArticleShown).each(function(post, index){ %>
    <%- partial('_widget/index-items', {index: index, post: post}) %>
  <% }) %>
<% } else { %>
  <% page.posts.each(function(post, index){ %>
    <%- partial('_widget/index-items', {index: index, post: post}) %>
  <% }) %>
<% } %>

修改后的archive.ejs:

<!-- 两篇文章以上 -->
<% if (pagination >= 2){ %>
  <!-- 置顶文章 -->
  <% page.posts.each(function(post, index){ %>
    <% if (post.top){ %>
      <%- partial('_widget/index-items', {index: index, post: post}) %>
    <% } %>
  <% }) %>
  <!-- 首页默认取最最新文章集 -->
  <% page.posts.sort('date', theme.homePageSortType).limit(theme.homeArticleShown).each(function(post, index){ %>
    <!-- 其余文章 -->
    <% if (!post.top){ %>
      <%- partial('_widget/index-items', {index: index, post: post}) %>
    <% } %> 
  <% }) %>
<% } else { %>
  <% page.posts.each(function(post, index){ %>
    <%- partial('_widget/index-items', {index: index, post: post}) %>
  <% }) %>
<% } %>

原来的category-archive.ejs:

<% if (pagination == 2){ %>
  <!-- 首页默认取最最新文章集 -->
  <% page.posts.sort('date', theme.homePageSortType).limit(theme.homeArticleShown).each(function(post, index){ %>
    <%- partial('_widget/category-items', {index: index, post: post}) %>
  <% }) %>
<% } else { %>
  <% page.posts.each(function(post, index){ %>
    <%- partial('_widget/category-items', {index: index, post: post}) %>
  <% }) %>
<% } %>

修改后的category-archive.ejs:

<% if (pagination >= 2){ %>
    <!-- 置顶文章 -->
    <% page.posts.each(function(post, index){ %>
      <% if (post.top){ %>
        <%- partial('_widget/category-items', {index: index, post: post}) %>
      <% } %>
    <% }) %>
    <!-- 首页默认取最最新文章集 -->
    <% page.posts.sort('date', theme.homePageSortType).limit(theme.homeArticleShown).each(function(post, index){ %>
      <!-- 其余文章 -->
      <% if (!post.top){ %>
        <%- partial('_widget/category-items', {index: index, post: post}) %>
      <% } %> 
    <% }) %>
<% } else { %>
  <% page.posts.each(function(post, index){ %>
    <%- partial('_widget/category-items', {index: index, post: post}) %>
  <% }) %>
<% } %>

5. 重新部署

hexo clean 
hexo g
hexo d

坚持