如何使用 Jekyll 中的属性更改页面顺序

How to change page orders using attributes in Jekyll

我想制作一个导航栏,其中的项目按优先顺序排列,以便 link 到页面。我在页面上添加了一个属性 "priority"。我编写了如下代码以按布局 HTML 的优先级排序,但页面未排序。为什么?

{% assign sorted_pages = site.pages | sort: "priority" %}

布局HTML和页面如下:

default.html

  <nav class="main-nav">
    <div class="main-menu">
      <ul id="main-menu" class="menu">
        {% assign sorted_pages = site.pages | sort: "priority" %}
        {% for post in sorted_pages %}
            <li class="menu-item"><a class="menu-link" href="{{ post.permalink }}">{{ post.title }}</a></li>
        {% endfor %}
      </ul>
    </div>
  </nav>

page1.md

---
layout: page
title: "Page1"
priority: 0
permalink: "aaa/bbb/page1.html"
---

# Page1 #
...

page2.md

---
layout: page
title: "Page2"
priority: 1
permalink: "aaa/bbb/page2.html"
---

# Page2 #
...

试试这个:

{% assign sorted_pages = site.pages | sort: "priority" %}
{% for post in sorted_pages %}
    <li class="menu-item"><a class="menu-link" href="{{ post.permalink }}">
    {% if post.priority %}{{ post.priority }} - {% endif %}
    {{ post.title }}</a></li>
{% endfor %}

然后看是顺序反了:

{% assign sorted_pages = site.pages | sort: "priority" | reverse %}