【发布时间】:2023-04-05 19:33:01
【问题描述】:
我有 Jekyll 博客,其中有些帖子有“特色图片”,而有些帖子没有。
特色图片在帖子的开头部分定义如下:featured-image: http://path/to/img
在存档页面上,我想抓取三个具有特色图片的最新帖子并显示它们。
我想这需要一个 if 语句、一个计数器和一个循环,但我无法让这个为我工作:
<ul id="archive-featured">
{% assign count = '0' %}
{% if count < '4' %}
{% for post in site.posts %}
{% if post.featured-image == true %}
{{ count | plus: '1' }}
<li><a href="{{ post.url }}"><img src="{{post.featured-image}}" />{{ post.title }}</a></li>
{% endif %}
{% endfor %}
{% endif %}
</ul>
我错过了什么?
【问题讨论】:
-
我也在寻找类似的东西 - > 如果计数为 0,我将完全跳过显示“相关”标题。所以我想先计数,然后循环如果 count > 0,则通过它们。
标签: jekyll