Triphys
Triphys

Reputation: 55

Jekyll: Listing all categories but one with a limit breaks the limit

I am trying to output a list of post titles with the 5 latest posts in ALL categories BUT one. It works fine with the filtering, unfortunately it takes the posts that are being excluded in consideration for the limit. Is there any easy way to not count the excluded posts?

{% for post in site.posts limit: 5 %}
    {% if post.category == "news" %}
    //Do nothing
    {% else if %}
        {{ post.title }}
    {% endif %}
{% endfor %}

Upvotes: 2

Views: 129

Answers (2)

marcanuy
marcanuy

Reputation: 23982

To limit filtered posts you need to filter them first.

We create an array of posts that do not include the news category:

{% assign no_news = "" | split: "" %}
{% for post in site.posts %}
{% unless post.categories contains 'news' %}
{% assign no_news = no_news | push: post %}
{% endunless %}
{% endfor %}

Now we traverse this new array limiting iterations by 5:

{% for post in no_news limit: 5 %}
        {{ post.title }} - {{post.categories}}
{% endfor %}

It will output 5 posts not containing the news category.

Upvotes: 2

Brad West
Brad West

Reputation: 969

You should be able to assign the posts first, then call them:

{% assign relevant = site.posts | except:"category","news" %}
{% for posts in relevant limit:5 %}
  {{ post.title }}
{% endfor %}

For more: https://github.com/jekyll/jekyll/issues/2018

Upvotes: 0

Related Questions