Jekyll 如何限制由前台过滤的帖子?
我有杰基尔的博客,有些帖子有“特色图片”,有些帖子没有 特色图片在帖子的前面定义如下:Jekyll 如何限制由前台过滤的帖子?,jekyll,Jekyll,我有杰基尔的博客,有些帖子有“特色图片”,有些帖子没有 特色图片在帖子的前面定义如下:特色图片:http://path/to/img 在归档页面上,我想抓取三篇最新的文章,其中有一些图片并显示出来 我想这将需要一个if语句、一个计数器和一个循环,但我无法让这一个为我工作: <ul id="archive-featured"> {% assign count = '0' %} {% if count < '4' %} {% for post in site.pos
特色图片:http://path/to/img
在归档页面上,我想抓取三篇最新的文章,其中有一些图片并显示出来
我想这将需要一个if语句、一个计数器和一个循环,但我无法让这一个为我工作:
<ul id="archive-featured">
{% assign count = '0' %}
{% if count < '4' %}
{% for post in site.posts %}
{% if post.featured-image == true %}
{{ count | plus: '1' }}
<li><a href="{{ post.url }}"><img src="{{post.featured-image}}" />{{ post.title }}</a></li>
{% endif %}
{% endfor %}
{% endif %}
</ul>
{%assign count='0'%}
{%如果计数<'4%}
{site.posts%中的post为%s}
{%if post.featured-image==true%}
{{count | plus:'1'}}
{%endif%}
{%endfor%}
{%endif%}
我遗漏了什么?你的YAML问题看起来不错,但我不确定你是否需要计数任务来完成这项工作。尝试使用“限制”来限制您在存档页面上分配的帖子数量。您也不需要为
{%if%}
语句指定“true”值即可工作:
<ul id="archive-featured">
{% for post in site.posts limit:3 %}
{% if post.featured-image %}
<li>
<a href="{{ post.url }}">
<img src="{{ post.featured-image }}" />
{{ post.title }}
</a>
</li>
{% endif %}
{% endfor %}
</ul>
{站点中的帖子的百分比。帖子限制:3%}
{%if post.featured-image%}
-
{%endif%}
{%endfor%}
我相信这些帖子是由最近的帖子自动显示的,所以不需要在那里做额外的工作。希望这有帮助 非常晚的回答:我认为问题在于
{{count | plus:1}}
。这不是只输出计数+1,而不分配它吗?
您可以通过在forloop结束之前分配一个新变量来解决这个问题
<ul id="archive-featured">
{% assign count = 0 %}
{% if count < 4 %}
{% for post in site.posts %}
{% if post.featured-image == true %}
<li><a href="{{ post.url }}"><img src="{{post.featured-image}}" />{{ post.title }}</a></li>
{% count | plus: 1 %}
{% endif %}
{% endfor %}
{% endif %}
</ul>
嗨,朱莉娅!谢谢回复!我尝试了一个早期的版本,但是如果最近的一篇文章没有特色图片,就会出现问题。只显示了两个员额。我想有三个最新的职位(有特色的图像)显示!这就是我为什么要尝试计数器+循环的原因。啊!我将用
{%if%}
语句包装
,而不是整个
。这个限制应该考虑显示最近的三张图片的问题,但是它会显示两张图片和一个链接。我想做的是,如果一篇文章没有特色图片,我们会跳过它,然后检查下一篇,然后继续,直到我们收集了三篇有特色图片的文章。然后打印出来。这有意义吗?我也在寻找这样的东西->如果计数为0,我将跳过显示“相关”标题。所以我想先数数,然后在计数大于0时循环遍历它们。
<ul id="archive-featured">
{% assign posts=site.posts | where "featured", "true" %}
{% for post in posts | limit: 3%}
<li><a href="{{ post.url }}"><img src="{{post.featured-image}}" />{{ post.title }}</a></li>
{% endfor %}
</ul>