<p>You can <a href="http://buildwithcraft.com/docs/matrix-fields#filtering-by-block-type">output the first matrix block of it's type</a> using <code>.type(blockHandle)</code>:</p>

<pre><code>{% 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 %}
</code></pre>

CC BY-SA 3.0
2014-09-29T11:16:31.117Z
Thanks for a great answer Mike! I'm assuming that you didn't use a "for block..." loop because using ".first" doesn't return an array?
user1070143
CC BY-SA 3.0
2014-09-29T12:57:26.357Z
Yep, ".first" will return either a single MatrixBlockModel or null, in which case the {% if photoBlock %} check will fail and nothing will be output. Same thing for the photo inside it, although if tactics.photo is a required field you can probably skip that check.
Mike Pepper
CC BY-SA 3.0
2014-09-29T10:45:02.713Z
2014-09-29T10:45:02.713Z
Mike Pepper
<p>You can exit the loop on some condition:</p>

<pre><code>{% for entry in craft.entries.section('mySection') %}

    {% set exitLoop = 0 %}
    {% for block in entry.tactics if exitLoop == 0 %}

        {% 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;

            {% set exitLoop = 1 %} 
            {# so as this loop will no longer be executed until new entry found #}

        {% endif %}

    {% endfor %}

{% endfor %}
</code></pre>

<p>Alternatively, you can use <a href="https://github.com/marionnewlevant/craft-mnbreakandcontinue" rel="nofollow">this</a> plugin to break the loop. In this case you can just add</p>

<pre><code>{% break %}
</code></pre>

<p>to the end of your <code>if</code> statement.</p>

CC BY-SA 3.0
2014-09-29T11:47:34.763Z
Thank you for the answer Nicael - I Learnt something new, but will go with Mikes answer because it's more crafty.
user1070143
CC BY-SA 3.0
2014-09-29T11:48:57.160Z
@user1070143 sure, it is better (because of length).
nicael
CC BY-SA 3.0
2014-09-29T10:32:07.033Z
2014-09-29T11:08:47.460Z
2014-09-29T11:08:47.460Z
nicael
<p>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.</p>

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

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

<pre><code>{% 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 %}
</code></pre>

<assets><matrix>
How do I output a matrix block type only once when looping through entries?
# How do I output a matrix block type only once when looping through entries?

(Asked by: username_0 on 2014-09-29)

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

            

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

                

                {{ entry.title }}
            

        {% endif %}

    {% endfor %}

{% endfor %}

**Tags:** assets, matrix

---

## 2 answers

### Answer by username_1 on 2014-09-29. Score: 5 | Accepted answer

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 %}
        
            {% set photo = photoBlock.photo.first() %}

            {% if photo %}
                
            {% endif %}

            {{ entry.title }}
        
    {% endif %}
{% endfor %}

> **Comments:**
> **username_0**: Thanks for a great answer Mike! I'm assuming that you didn't use a "for block..." loop because using ".first" doesn't return an array?  
> **username_1**: Yep, ".first" will return either a single MatrixBlockModel or null, in which case the {% if photoBlock %} check will fail and nothing will be output. Same thing for the photo inside it, although if tactics.photo is a required field you can probably skip that check.  

---

### Answer by username_2 on 2014-09-29. Score: 1

You can exit the loop on some condition:
{% for entry in craft.entries.section('mySection') %}

    {% set exitLoop = 0 %}
    {% for block in entry.tactics if exitLoop == 0 %}

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

            

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

                

                {{ entry.title }}
            

            {% set exitLoop = 1 %} 
            {# so as this loop will no longer be executed until new entry found #}

        {% endif %}

    {% endfor %}

{% endfor %}

Alternatively, you can use this plugin to break the loop. In this case you can just add
{% break %}

to the end of your if statement.

> **Comments:**
> **username_0**: Thank you for the answer Nicael - I Learnt something new, but will go with Mikes answer because it's more crafty.  
> **username_2**: @user1070143 sure, it is better (because of length).  

---

