Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jekyll, Liquid - Get all pages from category from page

Tags:

jekyll

liquid

I have a question in Jekyll Liquid.

I have layout, where I want to show pages from category. To show category I use page.categories variable. When I show in bracket {{page.categories}} is correct. but I don't know, how to pass to loop?

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

Don't work.

If I passed explicite:

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

It works.

I found another topic:

Jekyll site.categories.{{variable}}?

But it doesn't work.

like image 682
IceManSpy Avatar asked May 28 '14 14:05

IceManSpy


2 Answers

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.

like image 176
Christian Specht Avatar answered Sep 28 '22 08:09

Christian Specht


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>
like image 20
IceManSpy Avatar answered Sep 28 '22 08:09

IceManSpy