列出两个类别中的所有帖子(Jekyll / Liquid)
List all posts in both categories (Jekyll / Liquid)
我需要在 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 语句把事情搞砸了。 有没有办法 select 两个类别中都存在的帖子?
这是我的实际代码:
{% 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
这个 returns 列表过滤了两个类别:
{% assign list = site.categories.cooking | where_exp: "post", "post.categories contains 'pictures'" %}
您甚至可以链接多个 where_exp
过滤器以过滤三个、四个或更多类别。
我需要在 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 语句把事情搞砸了。 有没有办法 select 两个类别中都存在的帖子?
这是我的实际代码:
{% 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
这个 returns 列表过滤了两个类别:
{% assign list = site.categories.cooking | where_exp: "post", "post.categories contains 'pictures'" %}
您甚至可以链接多个 where_exp
过滤器以过滤三个、四个或更多类别。