Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

limit the number of post though a category with jekyll

Tags:

jekyll

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?

like image 937
Yannick Schall Avatar asked Sep 29 '12 17:09

Yannick Schall


2 Answers

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 %}
like image 96
Yannick Schall Avatar answered Jan 13 '23 04:01

Yannick Schall


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

{% for post in site.categories.company limit: 1 %}
  ...
{% endfor %}
like image 24
Nowaker Avatar answered Jan 13 '23 04:01

Nowaker