Jekyll, Liquid - Get all pages from category from page

泪湿孤枕 提交于 2019-12-05 11:05:54

page.categories is a list (see Page Variables), so you need to loop through it first and pass each category to the loop from your question:

{% 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 %}

This will first display all posts for the page's first category in descending order, then all posts for the page's second category in descending order, and so on.

Thank you. It's work.

Also I can use this code (use first on element of array, because in my case I have only one category per page):

{% 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>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!