我想循环浏览网站上的帖子,但类别为未列出
的帖子除外。我可以通过在 for 循环中嵌套 if 语句来实现此目的,但当我还想指定一个 limit
时,这种方法就会失效 - 无论 post 是否执行,循环都只会运行 5 次通过检查。
{% 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>
关于jekyll - 循环遍历液体中不属于某个类别的所有帖子列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35974002/