【发布时间】:2016-03-11 17:28:04
【问题描述】:
我正在使用 Jekyll 构建一个站点,并尝试使用 Liquid 逻辑在每个帖子的末尾创建一个“最近的帖子”数组。
我希望这个数组包含所有帖子除了您当前所在页面的帖子。
所以,我开始:
{% 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 过滤器接受 != 逻辑。
我怎样才能成功解决这个问题?
【问题讨论】: