我想制作一个包含优先顺序项目的导航栏,以建立页面链接。我添加了一个属性" 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 #
...
答案 0 :(得分:3)
试试这个:
{% 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 %}