按标签列出 Jekyll Collection 个页面
Listing Jekyll Collection pages by tags
我正在尝试在 collection 中创建一个按标签分组的页面列表。我知道这很容易通过 Jekyll 的 built-in site.tags
功能实现,我可以(并且已经)通过以下方式实现:
<h3>Tags</h3>
{% for tag in site.tags %}
{% assign t = tag | first %}
{% assign posts = tag | last %}
<h4><a name="{{t | downcase | replace:" ","-" }}"></a><a class="internal" href="/tagged/#{{t | downcase | replace:" ","-" }}">{{ t | downcase }}</a></h4>
<ul>
{% for post in posts %}
{% if post.tags contains t %}
<li>
<a href="{{ post.url }}">{{ post.title }}</a>
<span class="date">{{ post.date | date: "%B %-d, %Y" }}</span>
</li>
{% endif %}
{% endfor %}
</ul>
<hr>
{% endfor %}
为了得到这个:
我想在名为 note
的 Collection 中复制 site.tags
函数。我的 collection 中的标签被分组,就像它们用于帖子一样,使用例如 YAML header 中的 tags: [urban growth, California, industrialization]
。但我想让它与 Collection 一起工作。我几乎可以通过以下方式实现我想要的:
{% assign collection = site.note | group_by: "tags" | uniq %}
{% for group in collection %}
{% comment %} This is super hacky and I don't like it {% endcomment%}
<h3>{{ group.name | replace: '"', '' | replace: '[', '' | replace: ']', '' }}</h3>
<ul>
{% for item in group.items %}
<li><a href="{{ item.url | prepend: site.baseurl | prepend: site.url }}">{{ item.title }}</a></li>
{% endfor %}
</ul>
{%endfor%}
但是正如您可能看到的那样,这并没有将标签分成各自独特的组;相反,YAML 中的每组标签都被视为单个大标签。关于构建唯一标签数组并在其下列出 Collection 页面的任何指示?
试试这个:
{% assign tags = site.note | map: 'tags' | join: ',' | split: ',' | uniq %}
{% for tag in tags %}
<h3>{{ tag }}</h3>
<ul>
{% for note in site.note %}
{% if note.tags contains tag %}
<li><a href="{{ site.baseurl }}{{ note.url }}">{{ note.title }}</a></li>
{% endif %}
{% endfor %}
</ul>
{% endfor %}
David Jacquel的回答中提到的tags
赋值方法可以简化为:
{% assign tags = site.note | map: 'tags' | uniq %}
我正在尝试在 collection 中创建一个按标签分组的页面列表。我知道这很容易通过 Jekyll 的 built-in site.tags
功能实现,我可以(并且已经)通过以下方式实现:
<h3>Tags</h3>
{% for tag in site.tags %}
{% assign t = tag | first %}
{% assign posts = tag | last %}
<h4><a name="{{t | downcase | replace:" ","-" }}"></a><a class="internal" href="/tagged/#{{t | downcase | replace:" ","-" }}">{{ t | downcase }}</a></h4>
<ul>
{% for post in posts %}
{% if post.tags contains t %}
<li>
<a href="{{ post.url }}">{{ post.title }}</a>
<span class="date">{{ post.date | date: "%B %-d, %Y" }}</span>
</li>
{% endif %}
{% endfor %}
</ul>
<hr>
{% endfor %}
为了得到这个:
我想在名为 note
的 Collection 中复制 site.tags
函数。我的 collection 中的标签被分组,就像它们用于帖子一样,使用例如 YAML header 中的 tags: [urban growth, California, industrialization]
。但我想让它与 Collection 一起工作。我几乎可以通过以下方式实现我想要的:
{% assign collection = site.note | group_by: "tags" | uniq %}
{% for group in collection %}
{% comment %} This is super hacky and I don't like it {% endcomment%}
<h3>{{ group.name | replace: '"', '' | replace: '[', '' | replace: ']', '' }}</h3>
<ul>
{% for item in group.items %}
<li><a href="{{ item.url | prepend: site.baseurl | prepend: site.url }}">{{ item.title }}</a></li>
{% endfor %}
</ul>
{%endfor%}
但是正如您可能看到的那样,这并没有将标签分成各自独特的组;相反,YAML 中的每组标签都被视为单个大标签。关于构建唯一标签数组并在其下列出 Collection 页面的任何指示?
试试这个:
{% assign tags = site.note | map: 'tags' | join: ',' | split: ',' | uniq %}
{% for tag in tags %}
<h3>{{ tag }}</h3>
<ul>
{% for note in site.note %}
{% if note.tags contains tag %}
<li><a href="{{ site.baseurl }}{{ note.url }}">{{ note.title }}</a></li>
{% endif %}
{% endfor %}
</ul>
{% endfor %}
David Jacquel的回答中提到的tags
赋值方法可以简化为:
{% assign tags = site.note | map: 'tags' | uniq %}