对于Liquid中的循环:使用reverse和limit:1

Har*_*eve 19 liquid jekyll

我正在使用Jekyll建立一个简单的博客,我正在试图找出这个问题.

该网站的索引页面旨在展示一篇最新的文章,其结构类似于下面的内容(原谅这个混乱):

{% for post in site.posts reversed limit:1 %}
    <div class="post">
        <div class="post-inner">
            <h3 class="posttitle"><a href="{{ post.url }}">{{ post.title }}</a></h3>
            <p class="postdate">{{ post.date | date: "%d %B %Y" }}</p>
            {{ post.content }}
            <a href="{{ post.url }}#disqus_thread" class="commentLink"></a>
        </div>
    </div>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

当限制不是限制性的时(即不存在或设置为数组的长度),上述模板完全正常.似乎只有当限制实际上限制了循环忽略的结果时.

我已经尝试清除浏览器缓存,这是它无限制地工作的原因:1,但进度到此结束.

感谢您的帮助,如果这还不够,我很乐意提供更多细节.

小智 28

Jekyll实际上为你输出的是什么?

根据我的理解,反向过滤器最后应用.因此,假设你发布了8月的前15天,并且还让你说你做了这样的事情:

{% for post in site.posts limit:5 %}
    {{ post.content }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

您发布的数组将按以下模式排序

[8月15日,8月14日,8月13日,8月12日,8月11日]

然后,如果你扭转它

{% for post in site.posts reversed limit:5 %}
    {{ post.content }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

您发布的数组将按以下模式排序

[8月11日,8月12日,8月13日,8月14日,8月15日]

尽管如此,我对你为什么不使用感到有点困惑

{% for post in site.posts limit:1 %}
Run Code Online (Sandbox Code Playgroud)


sob*_*tel 10

通过自定义字段限制来自定义反向排序:

{% assign items = site.items | sort: 'some_field' | reverse %}
{% for item in items limit:10 %}
    <li><a href="{{ item.url }}">{{ item.title }}</a></li>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

分配给某些自定义var,排序(和反向)非常重要,然后才能进行限制循环.