Sorting/grouping collections 存档

Sorting/grouping collections for an archive

我正在构建我的第一个站点,并且遇到了非常困难的时期 grouping/sorting collections。我取得的唯一成功是按子目录的字母顺序排序的文件列表,然后按文件的日期(似乎是默认设置)使用:

<ul>
  {% for page in site.collection_name | sort: weight %}
      <a href="{{ page.url }}"><h3>{{ page.title }}</h3></a>{{ page.category }}{{page.excerpt}}
  {% endfor %}
</ul>

重量(以上)不起作用。类型、类别或我替换的任何其他变量也没有。

我的文件在子目录中,永久链接为:

/collection_name/category_name/file_name/

前言包括: 标题, 类别, 布局, 类型, (尝试了其他几个)

collection 将有多种文件类型,例如: 文章, 视频, 研究

我想要完成的是在我的 collection 中首先按类别循环,然后按类型循环。在非常糟糕的伪代码中:

<ul>
{% for page in site.{{category_name}} %}
<li><h2>{{category01}}</h2></li>

    <ul>
    {% for type in site.{{category_name.type}} | sort: date reverse%}
    <li><h3>Articles</h3></li>
        <li><a href="{{ page.url }}">{{ page.title }}</a>{{page.excerpt}}</li>

    <li><h3>Videos</h3></li>
        <li><a href="{{ page.url }}">{{ page.title }}</a>{{page.excerpt}}</li>

    <li><h3>Research</h3></li>
      <li><a href="{{ page.url }}">{{ page.title }}</a>{{page.excerpt}}</li>
    </uL>

<li><h2>{{category02}}</h2></li>
. . .

   . . . {% endfor %}

如有任何帮助或指导,我们将不胜感激。

尝试使用group_by :

{% assign byCategory = site.collection_name | group_by: 'category' | sort: 'name' %}
{% for cat in byCategory %}
  <h2>{{ cat.name | capitalize }}</h2>
  {% assign byType = cat.items | group_by: 'type' %}
  {% for type in byType %}
    <h3>{{ type.name | capitalize }}</h3>
    <ul>
      {% for item in type.items %}
        <li>{{ item.title }}</li>
      {% endfor %}
    </ul>
  {% endfor %}
{% endfor %}

注意:这适用于 category: mycategory 不适用于 categories: [one, two]

如果你想专门订购类型,你可以这样做:

_config.yml

# this array determine types order
# a collection itm with non matching type will not be listed
types :
  - articles
  - videos
  - research

代码

{% assign byCategory = site.area | group_by: 'category' %}
{% for cat in byCategory %}
  <h2>{{ cat.name }}</h2>
  {% assign byType = cat.items | group_by: 'type' %}
  {% for type in site.types %}
    {% assign currentType = byType | where:"name", type | first %}
    <h3>{{ currentType.name | capitalize }}</h3>
    <ul>
      {% for item in currentType.items %}
          <li>{{ item.title }}</li>
      {% endfor %}
    </ul>
  {% endfor %}
{% endfor %}