设置要在jekyll中显示的帖子的限制

设置要在jekyll中显示的帖子的限制,jekyll,Jekyll,我需要限制从许多帖子的集合中显示在列表中的帖子数量。我目前正在用一个特定名称的类别检查每个帖子,然后在找到前4篇帖子后打破循环 {% assign count = 0 %} {% for post in site.faqs %} {% if post.faq_category contains "category-name" %} {% assign count = count | plus:1} {% if count == 4 %} {% break %}

我需要限制从许多帖子的集合中显示在列表中的帖子数量。我目前正在用一个特定名称的类别检查每个帖子,然后在找到前4篇帖子后打破循环

{% assign count = 0 %}
 {% for post in site.faqs %}
  {% if post.faq_category contains "category-name" %}
   {% assign count = count | plus:1}
    {% if count == 4 %}
    {% break %}
        <li>{{ post.title }}</li>
    {% endif %}
{% endfor %}
{%assign count=0%}
{site.faqs%%中的帖子%
{%如果post.faq_类别包含“类别名称”%}
{%assign count=count | plus:1}
{%如果计数=4%}
{%break%}
  • {{post.title}}
  • {%endif%} {%endfor%}

    然而,这并没有返回任何结果。我不确定我是否只是把计数放错了位置或是什么。

    我发现我必须将计数设置在
    li
    元素下面:

    {% assign count = 0 %}
          {% for post in site.faqs %}
            {% if post.faq_category contains "category-name" %}
              {% assign count = count | plus:1 %}
              <li>{{ post.title }}</li>
              {% if count == 4 %}{% break %}
              {% endif %}
              {% endif %}
          {% endfor %}
    
    {%assign count=0%}
    {site.faqs%%中的帖子%
    {%如果post.faq_类别包含“类别名称”%}
    {%assign count=count |加号:1%}
    
  • {{post.title}}
  • {%if count==4%}{%break%} {%endif%} {%endif%} {%endfor%}
    使用计数器,您可以过滤帖子,并在计数器小于4时显示,否则使用
    中断

    {% assign count = 0 %}
    {% for post in site.faqs %}
      {% if post.faq_category contains "category-name" %}
       {% assign count = count | plus:1}
        {% if count < 4 %}
        <li>{{ post.title }}</li>
        {% endif %}
    {% endfor %}
    

    您也可以只使用
    {%forpost-in-site.faqs-limit:4%}
    达到同样的效果?
    {% assign faqs = site.faqs | where_exp:"item",
    "item.faq_category contains 'category-name'" %}
    <ul>
    {% for post in faqs limit:4 %}
    <li>{{post.title}}</li>
    {% endfor %}
    </ul>