如果系列声明.html在杰基尔博客中



我有一个系列中的一些语言.html文件位于我的 jekyll 博客的_includes部分,粘贴在下面:

{% if page.series %}
{% assign count = '0' %}
{% assign idx = '0' %}
{% for post in site.posts reversed %}
{% if post.series == page.series %}
{% capture count %}{{ count | plus: '1' }}{% endcapture %}
{% if post.url == page.url %}
{% capture idx %}{{count}}{% endcapture %}
{% endif %}
{% endif %}
{% endfor %}
{% endif %}

虽然它确实连接了在帖子的 YAML 布局中指定为系列一部分的帖子,但它也会将其他帖子作为系列的一部分连接起来,即使它们在帖子的 YAML 前言中未被指定为"系列",尽管有 if 语句。 这导致我的所有帖子都是我不想要的一般"系列"的一部分。 我只想指定一些帖子作为系列的一部分,而其他帖子只是独特的一次性帖子。

我尝试了以下方法:

  1. 在 YAML 前言中根本没有指定系列。
  2. 设置系列:假
  3. 设置系列:无

编辑 :

您的帖子.html模板中实际发生的事情是,并非所有的html都是有条件打印的。

_includes/series.html 中的所有内容都以page.series != null为条件,但<div class="panel seriesNote">的内容始终是打印的。

您需要在_includes/series.html中的条件中移动此代码(所有<div class="panel seriesNote">[...]</div>(。

{% if page.series != null %}
[...]
{% endfor %}
<div class="panel seriesNote">  <<<< moved code
[...]
</div>                          <<<< end moved code
{% endif %}

结束编辑

如果你没有在你的前言中设置seriespage.seriesnil.

如果将其留空,则nil

。因此,{% if page.series != null %}将帮助您仅选择系列设置为值的页面。

关于计数器的注意事项:

{% assign count = '0' %}=> 计数是一个字符串

{% capture count %}{{ count | plus: 1 }}{% endcapture %}=> 计数是一个字符串

如果您这样做:

{% 分配计数 =0 %} => 计数是一个整数

{% 分配计数 = 计数| 加号: 1 %} => 计数是一个整数

当您在比较中使用计数器时,请注意这一点,它可能会通过尝试比较字符串和整数而导致错误。

最新更新