jekyll - 循环遍历液体中不属于某个类别的所有帖子列表

标签 jekyll liquid

我想循环浏览网站上的帖子,但类别为未列出的帖子除外。我可以通过在 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/

相关文章:

css - 是否可以在 Jekyll 中使用纯 CSS 而不是 SASS

html - 用于检测当前页面 URL 的液体标记?

Jekyll过滤器可替换内容中的正则表达式?

javascript - 在我的 Shopify Liquid 主题中添加并解析我自己的 JSON 对象

jekyll - 在 Jekyll 中显示文件名并突出显示 Pygments 代码

jekyll - 在 jekyll 博客中嵌入动态应用程序

http - 我如何在子目录中使用 Jekyll?

fonts - 如何向 Jekyll 网站添加自定义本地字体?

jekyll - 某些液体过滤器不执行过滤功能

ruby - 从 Jekyll 插件向页面添加属性