【问题标题】:Jekyll: can't we loop in the posts (and display the list) from each post?Jekyll:我们不能在每个帖子中循环输入帖子(并显示列表)吗?
【发布时间】:2018-02-03 15:11:51
【问题描述】:

我想在类别页面中显示帖子列表:此循环有效。 但我也想在每个帖子上显示这个列表:它是相同的循环,但它不起作用。

这是我的 Jekyll 循环:

{% for post in site.categories.[include.nomrecueil] reversed %}
  <li>{{ post.title }</li>
{% endfor %}

短版

我的循环不会根据使用的布局获取帖子。 我有以下结构:

categoryA
    | index.md
    | categoryB
        | index.md
        | _posts
            | article.md

我有两个包含相同循环的布局。我在categoryB/index.md 中的循环正在工作,但在article.md 中的循环不起作用。知道为什么吗?我们不能在每个帖子中循环输入帖子(并显示列表)吗?

详情

您可能需要更多信息,如果需要,可以查看the repo。循环位于line 35 of the include nav-poemes.html,它包含在两个布局中:a)recueil.html 和 b)poem.html。循环在调用recueil.html 布局的index.md 中正确输出,但在调用poem.html 布局的article.md 中不正确输出。

我检查了循环中的包含参数nomrecueil 是否很好地通过了两种布局。在具有相同循环的第二个布局中未获取任何帖子... 我是在错误地使用 Jekyll 还是一个错误?

【问题讨论】:

    标签: html ruby loops jekyll liquid


    【解决方案1】:

    我不知道为什么......但这有效(这一定与“包含”语句有关):

    {% assing includenomrecueil = include.nomrecueil %}
    <ul>
      {% for category in site.categories %}
        {% assign catfirst = category | first %}
        {% if includenomrecueil contains catfirst %}
          {% for posts in category %}
            {% for p in posts reversed %}
              <li>...</li>
            {% endfor %}
          {% endfor %}
        {% endif %}
      {% endfor %}
    </ul>
    

    在您的代码中,变量通过。我什至尝试在变量中添加空格并删除它们(以确保它是一个字符串)。值得注意的是它有效:

    {% for post in site.categories['fleurs-persistantes'] reversed %}
      <li>...</li>
    {% endfor %}
    

    即使{{ include.nomrecueil }} 输出fleurs-persistantes,同时这也不起作用:

    {% for post in site.categories[include.nomrecueil] reversed %}
      <li>...</li>
    {% endfor %}
    

    真的很奇怪!没看懂。

    【讨论】:

    • 是的,奇怪的是我的原始循环在类别页面上有效,但在单个帖子页面上无效......无论如何,我很高兴你的循环有效!非常感谢您参与并解决我的问题! :) 我会看看我是否可以简化一下循环。我必须尝试 Hugo 看看他们的循环是否更有效,但到目前为止,我有一个可用的 Jekyll 站点。
    • 提示:请参阅 Jekyllcodex.org 获取不带插件的解决方案。
    猜你喜欢
    • 2021-05-05
    • 2020-08-27
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 2019-08-24
    • 1970-01-01
    相关资源
    最近更新 更多