使用jekyll限制帖子的数量

Yan*_*all 15 jekyll

我正试图用jekyll过滤一些帖子.我想输出所有类别的帖子:新闻.

当我这样做时它工作正常:

  {% for post in site.posts   %}
    {% if post.category[0] == "news" %}
      <h1>{{ post.title }}</h1>
  {% endfor %}
Run Code Online (Sandbox Code Playgroud)

但我想将此过滤器的输出限制为多个帖子.如果我将一个应用limit: 5到我的for循环,它不起作用,因为Jekyll将限制应用于帖子的总数.

无论如何都可以对已经过滤的帖子列表应用限制,例如:

  {% for post in site.posts   %}
    {% if post.category[0] == "news" limit:5 %}
      <h1>{{ post.title }}</h1>
  {% endfor %}
Run Code Online (Sandbox Code Playgroud)

我可以获取类别site.categories列表并列出它们

{% for category in site.categories %}
  <p>test: {{ category[0] }}</p>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

但我似乎无法缩小到一个类别.我正在尝试做类似的事情:

for post in site.categories.news limit:5
  //do something
endfor
Run Code Online (Sandbox Code Playgroud)

要么

for post in site.categories['news'] limit:5
  //do something
endfor
Run Code Online (Sandbox Code Playgroud)

但无济于事.是否可以通过这种方式过滤类别?

Yan*_*all 40

我设法解决它.

通过浏览jekyll bottstrap文档,site.categories.newstag: news在所有新闻页面上添加了一个我无法访问已过滤的帖子列表.

我现在可以过滤并限制帖子的输出:

  {% for post in site.tags.news limit:2  %}
    //do something
  {% endfor %}
Run Code Online (Sandbox Code Playgroud)


Now*_*ker 5

我的代码可以正常工作。看起来他们自 2012 年以来就在某个地方修复了这个问题。

{% for post in site.categories.company limit: 1 %}
  ...
{% endfor %}
Run Code Online (Sandbox Code Playgroud)