【问题标题】:Jekyll, Liquid - Get all pages from category from pageJekyll, Liquid - 从页面中获取类别中的所有页面
【发布时间】:2014-05-28 14:37:54
【问题描述】:

我有一个关于 Jekyll Liquid 的问题。

我有布局,我想在其中显示类别中的页面。为了显示类别,我使用 page.categories 变量。当我在括号中显示时 {{page.categories}} 是正确的。 但我不知道,如何传递给循环?

{% for post in site.categories[page.categories] %}
    <li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}


{% for post in site.categories[{{page.categories}}] %}
    <li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}

不工作。

如果我通过了显式:

{% for post in site.categories['cat1'] %}
    <li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}

有效。

我发现了另一个话题:

Jekyll site.categories.{{variable}}?

但它不起作用。

【问题讨论】:

    标签: jekyll liquid


    【解决方案1】:

    page.categories 是一个列表(请参阅Page Variables,因此您需要先遍历它,然后将每个类别传递给您的问题的循环:

    {% for cat in page.categories %}
      <h1>{{ cat }}</h1>
      <ul>
        {% for post in site.categories[cat] %}
          <li><a href="{{ post.url }}">{{ post.title }}</a></li>
        {% endfor %}
      </ul>
    {% endfor %}
    

    这将首先按降序显示页面第一个类别的所有帖子,然后按降序显示页面第二个类别的所有帖子,依此类推。

    【讨论】:

      【解决方案2】:

      谢谢。这是工作。

      我也可以使用此代码(首先在数组元素上使用,因为在我的情况下,我每页只有一个类别):

      {% assign pcat = page.categories %}
      
      <ul>
          {% for post in site.categories[pcat.first] %}
              <li {% if post.url == page.url %}class="active"{% endif %}><a href="{{ post.url }}">{{ post.title }}</a></li>
          {% endfor %}
      </ul>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多