【发布时间】:2015-01-22 23:00:34
【问题描述】:
我无法弄清楚如何按类别对集合的内容进行排序。我发现了一个关于如何按类别对帖子进行排序的非常相似的问题,但它似乎不适用于集合Jekyll display posts by category。
假设我有一个名为“汽车”的集合,有两个类别:“旧”和“新”。如何仅显示“旧”类别的汽车?
【问题讨论】:
标签: jekyll
我无法弄清楚如何按类别对集合的内容进行排序。我发现了一个关于如何按类别对帖子进行排序的非常相似的问题,但它似乎不适用于集合Jekyll display posts by category。
假设我有一个名为“汽车”的集合,有两个类别:“旧”和“新”。如何仅显示“旧”类别的汽车?
【问题讨论】:
标签: jekyll
您可以像页面或帖子等任何其他对象一样对集合进行排序、分组或过滤。
_my_collection/mustang.md
---
category: 'old'
abbrev: tang
---
mustang content
_my_collection/corvette.md
---
category: 'new'
abbrev: vet
---
corvette content
{% assign newcars = site.my_collection | where: "category", "new" %}
{% assign allcarssorted = site.my_collection | sort: "category" %}
{% assign groups = site.my_collection | group_by: "category" | sort: "name" %}
这会将您的汽车分组,然后按类别的名称对组进行排序。
{% for group in groups %}
{{ group.name }}
{% for item in group.items %}
{{item.abbrev}}
{%endfor%}
{%endfor%}
【讨论】: