1
votes

I have an include which renders my posts in a certain format. I am passing the posts to the include like below:

{% include post-list.html posts=site.posts %}

However, I would like to filter out a certain category before passing it to the include. Does anyone have any ideas how I can achieve this?

1

1 Answers

4
votes

You can do it defining a variable containing an array with all the posts not containing a specific category and then passing that to includes, for example to filter posts containing the category jekyll:

  1. We create the array {% assign notjekyllposts = "" | split: "" %}
  2. Loop all posts {% for apost in site.posts %}
  3. Filter those that does not contain the jekyll category adding them to the array:
    {% assign notjekyllposts = notjekyllposts | push: apost %}
  4. Pass the variable to the include tag

Putting it all together:

{% assign notjekyllposts = "" | split: "" %}

{% for apost in site.posts %}
  {% unless apost.categories contains 'jekyll' %}
    {% assign notjekyllposts = notjekyllposts | push: apost %}
  {% endunless %}
{% endfor %}
{% include post-list.html posts=notjekyllposts %}