Jekyll for循环的条件限制结果

Jekyll for循环的条件限制结果,jekyll,liquid,Jekyll,Liquid,假设他们的show属性为true,我想在jekyll博客中显示最后10篇文章 {% assign publishedPosts = site.posts | where: 'show', 'true' %} e、 g.YAML前方的物质可能是这样的 --- title: "SO question" categories: question show: false --- 在我的index.html文件中,我当前有以下内容 {% for post in site.posts limit:10

假设他们的
show
属性为
true
,我想在jekyll博客中显示最后10篇文章

{% assign publishedPosts = site.posts | where: 'show', 'true' %}
e、 g.YAML前方的物质可能是这样的

---
title: "SO question"
categories: question
show: false
---
在我的
index.html
文件中,我当前有以下内容

{% for post in site.posts limit:10 %} 
  {% if post.show %}
    <!-- display post -->
  {% endif %}
{% endfor %}
{%用于站点中的帖子。帖子限制:10%}
{%if post.show%}
{%endif%}
{%endfor%}
但如果最后10篇文章中有一篇文章的
show
属性为
false
,那么页面上只会出现9篇文章

Jinja2支持if的
语法,如下所示:。这将解决我的问题,但不幸的是液体不支持


使用liquid如何调节post属性并确保始终显示10篇文章?

尝试使用
show
值设置为
true
的post计数器变量分配给
。下面是一些简单的方法之一

{% assign count = 0 %}
   {% for post in site.posts limit:10 %} 
     {% if post.show %}
         {% if count < 10 %} 
         <!-- display post -->
         {% increment count %}
         {% endif %}
   {% endif %}
{% endfor %}
{%assign count=0%}
{站点中的帖子的百分比。帖子限制:10%}
{%if post.show%}
{如果计数<10%,则为%0}
{%increment count%}
{%endif%}
{%endif%}
{%endfor%}
这应该行得通,但是我现在没有安装Jekyll来检查,或者如果增量不起作用,请使用
{%assign count=count+1%}

这个


或者,我觉得您只想设置一篇文章是否应该发布,在这种情况下,可以使用内置的
published
变量。只需在前台执行
published:true
published:false
。阅读有关预定义变量的更多信息。

您必须首先使用将
show
变量设置为
true
的帖子创建一个数组

{% assign publishedPosts = site.posts | where: 'show', 'true' %}
然后你可以做一个

{% for p in publishedPosts limit:10 %}

只需在@matrixanonaly-answer上指出,如果不能使用“where”过滤器,它可以很好地与小tweek配合使用。我还不能发表评论,但我想我会修复它,让它工作,因为它帮助了我

 {% assign count = 0 %}
   {% for post in site.posts limit:10 %} 
     {% if post.show %}
         {% if count < 10 %} 
         <!-- display post -->
         {% assign count = count | plus: 1 %}
         {% endif %}
   {% endif %}
{% endfor %}
{%assign count=0%}
{站点中的帖子的百分比。帖子限制:10%}
{%if post.show%}
{如果计数<10%,则为%0}
{%assign count=count |加号:1%}
{%endif%}
{%endif%}
{%endfor%}

当我在标签中使用contains时,我需要这个。由于某种原因,增量不起作用。

不知道你可以用液体来做,很好+1.