遍历所有不属于 liquid 类别的帖子列表

Looping through all list of posts not in some category in liquid

我想遍历网站上的 posts,类别 unlisted 除外。我可以通过在 for 循环中嵌套一个 if 语句来做到这一点,但是当我还想指定一个 limit 时,这就会崩溃——循环将只 运行 5 次,无论是否post 通过检查。

{% for post in site.posts limit: 5 %}
  {% unless post.categories contains 'unlisted' %}
  <!-- display post -->
  {% endunless %}
{% endfor %}

我需要将一个已过滤的列表传递给 for 循环,但我无法这样做,主要是因为我找不到将 where 过滤器与 contains 组合的方法和否定:

{% for post in site.posts | WHERE CATEGORIES NOT CONTAINS 'UNLISTED' | limit: 5 %}
  <!-- display post -->
{% endfor %}

您可以使用计数器:

<ul>
{% assign postCounter = 0 %}
{% assign maxPost = 5 %}
{% for post in site.posts %}
  {% unless post.categories contains 'unlisted' %}
    <li><a href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a></li>
    {% assign postCounter = postCounter | plus: 1 %}
    {% if postCounter >= maxPost %}
      {% break %}
    {% endif %}
  {% endunless %}
{% endfor %}
</ul>