I want to include a conditional for when the 'Text' block type is returned for the first time in a loop. In css it would be first-of-type.

Here's what I have at the moment:

{% if type == 'text' and loop.first %}
    <div class="centered spacer-icon m-all t-all d-all"><img src="resources/img/minus-icon.svg" alt="Minus Icon" width="30" /></div>
{% endif %}

In this instance my text block type is loop index 3 but I want to adjust the conditional because it is the first text type in the matrix field.


You could first set a variable to test against and then change it the first time the block type appears.

{% set firstTime = true %}

{% for block in entry.matrixField %}

    {% if block.type == "text" and firstTime == true %}

        ...Do something once...

        {% set firstTime = false %}

    {% endif %}

{% endfor %}
| improve this answer | |

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.