我正在尝试输出某个作者的博客文章列表。我在Jekyll过滤器上尝试过此操作:
{% for post in (site.posts | where:"author", "mike") %}
{{ post.title }}
{% endfor %}
但是它输出每个帖子。我不清楚我在做什么错。
假设您的帖子作者是您的首要任务,例如:
---
author: toto
...
---
如果您希望作者== toto发表最后两篇文章,请执行以下操作:
{% assign counter = 0 %}
{% assign maxPostCount = 2 %}
<ul>
{% for post in site.posts %}
{% if post.author == 'toto' and counter < maxPostCount %}
{% assign counter=counter | plus:1 %}
<li>{{ counter }} - {{ post.title }}</li>
{% endif %}
{% endfor %}
</ul>
跳!
编辑:和另一个解决方案使用where过滤器,而不是if子句:
{% assign posts = site.posts | where: "author", "toto" %}
{% assign counter2 = 0 %}
{% assign maxPostCount2 = 3 %}
<ul>
{% for post in posts %}
{% if counter2 < maxPostCount2 %}
{% assign counter2=counter2 | plus:1 %}
<li>{{ counter2 }} - {{ post.title }}</li>
{% endif %}
{% endfor %}
</ul>
重新编辑:贾斯汀是正确的,我不需要我的两个变量(counter2和maxPostCount2),我可以将Liquid用于循环 limit:n选项。
{% assign posts = site.posts | where: "author", "toto" %}
<ul>
{% for post in posts limit:3 %}
<Ol>{{ post.title }}</ol>
{% endfor %}
</ul>
更好!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句