简体   繁体   中英

Filter Array Liquid in Jekyll

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM