我在Jekyll 3.2.1中创建了一个虚拟的“相关文章”模块,使用以下解决方案:
{% for post in site.posts limit: 4 %}
{% if page.author == post.author and page.title != post.title %}
<div class="post-stream-item">
<a href="{{ post.url | prepend: site.baseurl }}" title="{{ post.title }}"><div class="post-stream-content">
<img src="{{ post.thumbnail }}" width="80" height="auto" /><span class="post-stream-item-meta"><h3>{{ post.title }}</h3><p>{{ post.author }} on {{ post.date | date: "%b %-d, %Y" }} • {% assign words = post.content | number_of_words %}
{% if words <= 160 %}
1 min
{% else %}
{{ words | plus: 159 | divided_by:160 }} mins
{% endif %} read</p></span></div></a></div>{% if forloop.last == false %}<hr>{% endif %}
{% endif %}
{% endfor %}
- for循环遍历站点中的帖子列表并给出一个限制
- 如果当前帖子的作者与迭代帖子的作者相同,但标题不同,则填写jinja绑定。
问题出在 {% if forloop.last == false %}<hr>{% endif %}
这部分,因为如果for循环中有更多可迭代的(帖子),它将显示 <hr>
标签,即使它是最后一个展示给用户的元素。
是否有任何属性可以引用列表中倒数第二个元素或任何更好的解决方案?
if
语句的迭代。 - Kevin Workman