Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-29T11:16:31.117Z
Answers.Text: 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?
Answers.UserDisplayName: user1070143
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-29T12:57:26.357Z
Answers.Text: 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.
Answers.UserDisplayName: Mike Pepper
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-29T10:45:02.713Z
Answers.LastActivityDate: 2014-09-29T10:45:02.713Z
Answers.UserDisplayName: Mike Pepper
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-29T11:47:34.763Z
Answers.Text: Thank you for the answer Nicael - I Learnt something new, but will go with Mikes answer because it's more crafty.
Answers.UserDisplayName: user1070143
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-29T11:48:57.160Z
Answers.Text: @user1070143 sure, it is better (because of length).
Answers.UserDisplayName: nicael
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-29T10:32:07.033Z
Answers.LastActivityDate: 2014-09-29T11:08:47.460Z
Answers.LastEditDate: 2014-09-29T11:08:47.460Z
Answers.UserDisplayName: nicael
Body: <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>
  
Tags: <assets><matrix>
Title: How do I output a matrix block type only once when looping through entries?
ThreadText: # 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" %}
  
              <a href="{{ entry.Url }}">
  
                  {% set photo = block.photo.first() %}
  
                  <img src="{{ photo.getUrl }}" width="{{ photo.getWidth() }}" height="{{ photo.getHeight() }} alt="{{ entry.title }}"/>
  
                  {{ entry.title }}
              </a>
  
          {% 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](http://buildwithcraft.com/docs/matrix-fields#filtering-by-block-type) using `.type(blockHandle)`:
  
  ```
  {% for entry in craft.entries.section('mySection') %}
      {% set photoBlock = entry.tactics.type('photo').first %}
  
      {% if photoBlock %}
          <a href="{{ entry.Url }}">
              {% set photo = photoBlock.photo.first() %}
  
              {% if photo %}
                  <img src="{{ photo.getUrl }}" width="{{ photo.getWidth() }}" height="{{ photo.getHeight() }} alt="{{ entry.title }}"/>
              {% endif %}
  
              {{ entry.title }}
          </a>
      {% endif %}
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks for a great answer username_1! 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" %}
  
              <a href="{{ entry.Url }}">
  
                  {% set photo = block.photo.first() %}
  
                  <img src="{{ photo.getUrl }}" width="{{ photo.getWidth() }}" height="{{ photo.getHeight() }} alt="{{ entry.title }}"/>
  
                  {{ entry.title }}
              </a>
  
              {% set exitLoop = 1 %} 
              {# so as this loop will no longer be executed until new entry found #}
  
          {% endif %}
  
      {% endfor %}
  
  {% endfor %}
  
  ```
  
  Alternatively, you can use [this](https://github.com/marionnewlevant/craft-mnbreakandcontinue) 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 username_2 - I Learnt something new, but will go with Mikes answer because it's more crafty.  
  > **username_2**: @username_0 sure, it is better (because of length).  
  
  ---
  
  