2015-12-07 77 views
0

我正在使用Jekyll构建网站,并试图在每篇文章的末尾使用Liquid逻辑创建一个'Recent Posts'数组。用!=逻辑过滤Liquid/Jekyll数组?

我想这个数组包含所有帖子,除了您当前所在页面的帖子。

于是,我开始有:

{% for post in site.posts limit:2 %} 
    {% if post.title != page.title %} 
    //render the post 
    {% endif %} 
{% endfor %} 

这工作,但我的limit: 2导致的问题。由于Liquid限制之前的逻辑if,如果它真的遇到标题等于当前页面标题的帖子,它将(正确)不会渲染它,但它会认为限制“满意” - 我最终只有1相关岗位而不是2

接下来,我尝试创建我自己的讯息阵列:

{% assign currentPostTitle = "{{ page.title }}" %} 
{% assign allPostsButThisOne = (site.posts | where: "title" != currentPostTitle) %} 

{% for post in allPostsButThisOne limit:2 %} 
    //render the post   
{% endfor %} 

这不工作,因为我不能让where过滤器接受!=逻辑。

如何成功解决此问题?

回答

3

您可以使用计数器:

{% assign maxCount = 2 %} 
{% assign count = 0 %} 
<ul> 
{% for post in site.posts %} 
    {% if post.title != page.title and count < maxCount %} 
    {% assign count = count | plus: 1 %} 
    <li>{{ post.title }}</li> 
    {% endif %} 
{% endfor %} 
</ul> 
+0

优秀的,这正是我需要的东西 - 一个新的方式来思考解决问题。谢谢! – GloryOfThe80s