首页 文章

Jekyll按类别展示收藏品

提问于
浏览
12

我发现了一个关于如何按类别对帖子进行排序的非常相似的问题,但它似乎不适用于集合Jekyll display posts by category .

假设我有一个名为'cars'的集合,有两个类别:'old'和'new' . 如何仅显示“旧”类别的汽车?

1 回答

  • 20

    您可以像任何其他对象(如页面或帖子)一样对集合进行排序,分组或过滤 .

    _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%}
    

相关问题