How do I output a matrix block type only once when looping through entries? My matrix field has a block type called photo. When looping through entries I only want to output the first instance of this block type (although it will have been used multiple times), i.e. the first photo.

The code below loops through the entries, but for each entry it's outputting every photo for the entry - not just the first.

What do I need to change to get just the first photo of each entry output.

{% for entry in craft.entries.section('mySection') %}

    {% for block in entry.tactics %}

        {% if block.type == "photo" %}

            &lt;a href="{{ entry.Url }}"&gt;

                {% set photo = block.photo.first() %}

                &lt;img src="{{ photo.getUrl }}" width="{{ photo.getWidth() }}" height="{{ photo.getHeight() }} alt="{{ entry.title }}"/&gt;

                {{ entry.title }}
            &lt;/a&gt;

        {% endif %}

    {% endfor %}

{% endfor %}
You can output the first matrix block of it's type using .type(blockHandle):

{% for entry in craft.entries.section('mySection') %}
    {% set photoBlock = entry.tactics.type('photo').first %}

    {% if photoBlock %}
        &lt;a href="{{ entry.Url }}"&gt;
            {% set photo = photoBlock.photo.first() %}

            {% if photo %}
                &lt;img src="{{ photo.getUrl }}" width="{{ photo.getWidth() }}" height="{{ photo.getHeight() }} alt="{{ entry.title }}"/&gt;
            {% endif %}

            {{ entry.title }}
        &lt;/a&gt;
    {% endif %}
{% endfor %}