为什么这个Jekyll Liquid过滤器不在哪里过滤? [英] Why doesn't this Jekyll Liquid where filter filter?

查看:54
本文介绍了为什么这个Jekyll Liquid过滤器不在哪里过滤?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试输出某个作者的博客文章列表.我在Jekyll过滤器上尝试过这个:

I am trying to output a list of blog posts for a certain author. I tried this where Jekyll filter:

{% for post in (site.posts | where:"author", "mike") %}
  {{ post.title }}
{% endfor %}

但是它输出每个帖子.我不清楚我在做什么错.

But it outputs every post. I'm not clear what I'm doing wrong.

推荐答案

假设您的帖子作者是您的首要任务,例如:

Supposing that your post author is in your front matter, like this :

---
author: toto
...
---

如果您想要作者== toto撰写的最后两篇文章,请执行以下操作:

If you want two last post by author == toto, just do :

{% 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子句的解决方案:

EDIT : And another solution using the where filter instead of the if clause :

{% 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),我可以使用

RE- Justin is right I don't need my two vars (counter2 and maxPostCount2), I can use Liquid for loop limit:n option.

{% assign posts = site.posts | where: "author", "toto" %}
<ul>
{% for post in posts limit:3 %}
  <Ol>{{ post.title }}</ol>
{% endfor %}
</ul>

更好!

这篇关于为什么这个Jekyll Liquid过滤器不在哪里过滤?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆