[英]Jekyll, Liquid - Get all pages from category from page
我在Jekyll Liquid中有一个问题。
我有布局,我想从类别中显示页面。 要显示类别,我使用page.categories变量。 当我在括号{{page.categories}}中显示时是正确的。 但我不知道,如何传递给循环?
{% for post in site.categories[page.categories] %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
{% for post in site.categories[{{page.categories}}] %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
不要工作。
如果我通过explicite:
{% for post in site.categories['cat1'] %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
有用。
我找到了另一个主题:
Jekyll site.categories。{{variable}}?
但它不起作用。
page.categories
是一个列表(参见页面变量 ) ,因此您需要首先遍历它并将每个类别传递给您的问题循环:
{% for cat in page.categories %}
<h1>{{ cat }}</h1>
<ul>
{% for post in site.categories[cat] %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
</ul>
{% endfor %}
这将首先按降序显示页面的第一个类别的所有帖子,然后按降序显示页面的第二个类别的所有帖子,依此类推。
谢谢。 这是工作。
此外,我可以使用此代码(首先使用数组元素,因为在我的情况下,我每页只有一个类别):
{% assign pcat = page.categories %}
<ul>
{% for post in site.categories[pcat.first] %}
<li {% if post.url == page.url %}class="active"{% endif %}><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
</ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.