Jekyll按类别显示收藏 [英] Jekyll display collection by category
问题描述
我无法弄清楚如何按类别对集合的内容进行排序.我发现了一个关于如何按类别对帖子进行排序的非常相似的问题,但是它似乎不适用于
I'm having trouble figuring out how to sort the content of a collection by category. I've found a very similar question on how to sort posts by category but it doesn't seem to work with collections Jekyll display posts by category.
比方说,我有一个名为汽车"的收藏,分为两个类别:旧"和新".如何仅显示旧"类别的汽车?
Let's say I have a collection called 'cars' with two categories: 'old' and 'new'. How can I display only cars with the category 'old'?
推荐答案
您可以像对任何其他对象(如页面或帖子)那样对集合进行排序,分组或过滤.
You can sort, group or filter collection like any other object like page or posts.
_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" %}
这会将您的汽车分组,然后按类别(即类别)对分组进行排序.
This groups your cars and then sort groups by name that is the category.
{% for group in groups %}
{{ group.name }}
{% for item in group.items %}
{{item.abbrev}}
{%endfor%}
{%endfor%}
这篇关于Jekyll按类别显示收藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!