18

I'm trying to filter some post with jekyll. I want to output all post with category: news.

It works fine when i do:

  {% for post in site.posts   %}
    {% if post.category[0] == "news" %}
      <h1>{{ post.title }}</h1>
  {% endfor %}

but i'd like to limit the output to this filter to a number of posts. If I apply a limit: 5 to my for loop it doesn't work as Jekyll applies the limit to the total number of posts.

Is it in anyway possible to apply a limit to an already filtered list of posts, something like:

  {% for post in site.posts   %}
    {% if post.category[0] == "news" limit:5 %}
      <h1>{{ post.title }}</h1>
  {% endfor %}

I can get the list of categories with site.categories and list them

{% for category in site.categories %}
  <p>test: {{ category[0] }}</p>
{% endfor %}

But i can't seem to be able to narrow to a single category. I'm trying to do something like:

for post in site.categories.news limit:5
  //do something
endfor

or

for post in site.categories['news'] limit:5
  //do something
endfor

but to no avail. Is it possible to filter a category this way?

Yannick Schall
  • 32,601
  • 6
  • 29
  • 42

2 Answers2

46

I manage to sort it out.

I couldn't access the filtered list of post through site.categories.news I've added a tag: news on all the news page after looking through the jekyll bottstrap documentation.

I can now filter and limit the output of post with:

  {% for post in site.tags.news limit:2  %}
    //do something
  {% endfor %}
Yannick Schall
  • 32,601
  • 6
  • 29
  • 42
5

My code just works. Looks like they fixed it somewhere since 2012.

{% for post in site.categories.company limit: 1 %}
  ...
{% endfor %}
Nowaker
  • 12,154
  • 4
  • 56
  • 62