Search code examples
jekyllliquidgithub-pages

loop through filtered collection in jekyll


I try to do things like this in my post:

<ul class="articles-list">
  {% for post in site.posts | where:'group', post.group %}
    <div data-scroll-reveal="enter ease 0">
      {% include article-snippet.html %}
    </div>
  {% endfor %}
</ul>

but it loops through all my collection instead the only loops posts with special group.


Solution

  • You cannot use where filter in a loop.

    But you can do :

    {% assign posts = site.posts | where:'group', post.group %}
    
    <ul class="articles-list">
      {% for post in posts %}
        <div data-scroll-reveal="enter ease 0">
          {% include article-snippet.html %}
        </div>
      {% endfor %}
    </ul>