2015-12-07 79 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