I have a problem with {% if loop.first %} and {{ loop.index0 }} with the following snippet:

{% for block in entry.locationSlideshow %}
  {% switch block.type %}
    {% case 'upload' %}
      {% for image in block.image %}
        <button {% if loop.first %}class="is-active" {% endif %}data-slide="{{ loop.index0 }}"><span class="show-for-sr">{{ image.title }}</span></button>
      {% endfor %}
  {% endswitch %} 
{% endfor %}

The problem is that all buttons get the class is-active and 0 instead of 0, 1, 2, 3...

This is working fine outside of a Matrix field. Any ideas what I am doing wrong here?


1 Answer 1


At the moment loop.first and loop.index0 are relative to your block.image loop.

If you want them to be set based on your entry.locationSlideshow loop (the parent loop) you can use {{ loop.parent.loop.index0 }} and {{ loop.parent.loop.first }}

For more information see the Twig documentation.

  • Works like a charm. Thanks a lot for this precise explanation!
    – bennobo
    Jun 13, 2016 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.