Liquid 模板:获取符合标准的前 5 个帖子

Liquid template: Get first 5 posts that match criterion

我想显示最近的 5 个 post,它们在我的 jekyll-blog 主页上有一个缩略图。如何做到这一点?

我在 post 的 header 中将缩略图设置为属性:

---
layout: post
title: Lorem Ipsum
thumb: images/thumb.jpg
---

我试过了

{% for post in site.posts | sort:"date" | reverse | limit: 5 %}
    {% if post.thumb %}
        <img src= ... />
    {% endif %}
{% endfor %}

当然,如果这五个中有一个没有图片,则只会显示四个。有解决这个问题的顺利方法吗?

尝试:

{% assign maxPost = 5 %}
{% assign counter = 0 %}
{% for post in site.posts | sort:"date" | reverse %}
    {% if post.thumb %}
        <img src= ... />
        {% assign counter = counter | plus: 1 %}
        {% if counter == maxPost %}
            {% break %} {% comment %}exit the for loop{% endcomment %}
        {% endif %}
    {% endif %}
{% endfor %}