Jekyll display collection by category

前端 未结 1 1484
北恋
北恋 2020-12-23 23:29

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 se

相关标签:
1条回答
  • 2020-12-23 23:54

    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
    

    Filtering

    {% assign newcars = site.my_collection | where: "category", "new" %}
    

    Sorting

    {% assign allcarssorted = site.my_collection | sort: "category" %}
    

    Grouping

    {% 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%}
    
    0 讨论(0)
提交回复
热议问题