【发布时间】:2018-05-27 03:01:04
【问题描述】:
我需要在 for 循环中列出所有包含这两个类别的帖子。
{% assign cooking_pages = site.categories.cooking & site.categories.pictures %}
显然以上方法行不通,但这基本上是我想要做的。
我知道我能做到:
{% assign cooking_pages = site.categories.cooking | sort:"title"%}
{% for post in cooking_pages %}
{% if post.categories contains 'pictures' %}
Do whatever I want to do
{% endif %}
{% endfor %}
但是我的函数使用循环索引和取模,所以 if 语句把事情搞砸了。 有没有办法选择两个类别中都存在的帖子?
这是我的实际代码:
{% assign sorted_pages = (site.categories.2018 | sort:"date") | reverse %}
<table>{% for post in sorted_pages %}
{% assign loopindex = forloop.index | modulo: 3 %}
{% if loopindex == 1 %}
<tr><td id="entries"><strong><a href="{{ post.url }}">{{ post.title }}</a></strong></td>
{% elsif loopindex == 0 %}
<td id="entries"><strong><a href="{{ post.url }}">{{ post.title }}</a></strong></td></tr>
{% else %}
<td id="entries"><strong><a href="{{ post.url }}">{{ post.title }}</a></strong></td>
{% endif %}
{% endfor %}</table>
它在这个页面上给了我很好的表格:https://200wordrpg.github.io/2018entries
【问题讨论】: