gpt4 book ai didi

jekyll - 按标签列出 Jekyll Collection 页面

转载 作者:行者123 更新时间:2023-12-02 11:47:22 25 4
gpt4 key购买 nike

我正在尝试在集合中创建按标签分组的页面列表。我知道使用 Jekyll 的内置 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 %}

为了得到这个:

Grouped by tags

我想在名为 note 的集合中复制 site.tags 函数。我收集的标签像帖子一样进行分组,例如在 YAML header 中使用 tags: [urban Growth, California, Industrialization] 。但我想让它与集合一起工作。我几乎可以通过以下方式实现我想要的:

{% 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 中的每组标签都被视为单个大标签。有关构建唯一标签数组并在其下列出集合页面的任何指示吗?

最佳答案

试试这个:

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

关于jekyll - 按标签列出 Jekyll Collection 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36958975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com