<p>You can either use external bool variable</p>

<pre><code>{% set firstNbContentDisplayed = false %}
{% for block in entry.newsBlock %}
    {% switch block.type %}
        {% case 'nbTitle' %}
            ...
        {% case 'nbContent' %}
            {% if not firstNbContentDisplayed %} 
                {% set firstNbContentDisplayed = true %}
                ...
            {% endif %}
    {% endswitch %}
{% endfor %}
</code></pre>

<p>or if possible in your case separate nbContent and limit it to 1</p>

<pre><code>{% for block in entry.newsBlock %}
    {% switch block.type %}
        {% case 'nbTitle' %}
            ...
    {% endswitch %}
{% endfor %}

{% for block in entry.newsBlock.type('nbContent').limit(1) %}
    ...
{% endfor %}
</code></pre>

CC BY-SA 3.0
2017-07-11T00:48:31.310Z
Thanks! I ended up using your second option, and it worked. Thank you!!!
Ivy
CC BY-SA 3.0
2017-07-10T07:40:48.183Z
2017-07-10T07:40:48.183Z
Krzysztof Boduch
<p>I haven't tested this, but I think </p>

<pre><code>{% for block in entry.newsBlock.type('blockType').limit(1) %}
    {{ block.title }}
{% endfor %} 
</code></pre>

<p>ought to do it?</p>

<p>If not you could simply do what you're currently doing but also use a </p>

<pre><code>{% if loop.first %}
...
{% endif %}
</code></pre>

<p>inside it to only spit out content from the first loop iteration.</p>

CC BY-SA 3.0
2017-07-10T07:42:44.627Z
2017-07-10T12:56:29.460Z
2017-07-10T12:56:29.460Z
Matt Wilcox
<p>I've got a Matrix block in my Latest News/Index template and I can't figure out how to only output the first instance of a block type field.
Here is my code, working great except it outputs the first 250 characters from ALL <code>{{block.newsContent}}</code> content block fields not just the first. I know this is what my code is actually set up to do, but I can't figure out how to only output the first one!</p>

<pre><code>{% for block in entry.newsBlock %}
    {% switch block.type %}

        {% case 'nbTitle' %}

            &lt;a href="{{ entry.url }}"&gt; &lt;h2&gt; {{ block.newsTitle }} &lt;/h2&gt;&lt;/a&gt;

            &lt;h3&gt;{{ entry.postDate | date("d-M-Y") }}&lt;/h3&gt;


        {% case 'nbContent' %}

            &lt;div class="news-content-index"&gt;
                &lt;p&gt;{{ block.newsContent | striptags | slice(0, 250) ~ '...' }}&lt;/p&gt;
            &lt;/div&gt;

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

<p>The <code>{{block.newsContent}}</code> part outputs every instance of that field, but I only need the first, as it's the 'excerpt' intro to the complete article.</p>

<p>Here is the front end:
<a href="https://www.ancamotion.com/latest-news" rel="nofollow noreferrer">https://www.ancamotion.com/latest-news</a></p>

<p>Any help appreciated, thanks!</p>

<templating>
How to output only the first instance of a Matrix block type field
# How to output only the first instance of a Matrix block type field

(Asked by: username_0 on 2017-07-10)

I've got a Matrix block in my Latest News/Index template and I can't figure out how to only output the first instance of a block type field.
Here is my code, working great except it outputs the first 250 characters from ALL {{block.newsContent}} content block fields not just the first. I know this is what my code is actually set up to do, but I can't figure out how to only output the first one!
{% for block in entry.newsBlock %}
    {% switch block.type %}

        {% case 'nbTitle' %}

              {{ block.newsTitle }} 

            {{ entry.postDate | date("d-M-Y") }}


        {% case 'nbContent' %}

            
                {{ block.newsContent | striptags | slice(0, 250) ~ '...' }}
            

    {% endswitch %}
{% endfor %}

The {{block.newsContent}} part outputs every instance of that field, but I only need the first, as it's the 'excerpt' intro to the complete article.
Here is the front end:
https://www.ancamotion.com/latest-news
Any help appreciated, thanks!

**Tags:** templating

---

## 2 answers

### Answer by username_1 on 2017-07-10. Score: 3 | Accepted answer

You can either use external bool variable
{% set firstNbContentDisplayed = false %}
{% for block in entry.newsBlock %}
    {% switch block.type %}
        {% case 'nbTitle' %}
            ...
        {% case 'nbContent' %}
            {% if not firstNbContentDisplayed %} 
                {% set firstNbContentDisplayed = true %}
                ...
            {% endif %}
    {% endswitch %}
{% endfor %}

or if possible in your case separate nbContent and limit it to 1
{% for block in entry.newsBlock %}
    {% switch block.type %}
        {% case 'nbTitle' %}
            ...
    {% endswitch %}
{% endfor %}

{% for block in entry.newsBlock.type('nbContent').limit(1) %}
    ...
{% endfor %}

> **Comments:**
> **username_0**: Thanks! I ended up using your second option, and it worked. Thank you!!!  

---

### Answer by username_2 on 2017-07-10. Score: 3

I haven't tested this, but I think 
{% for block in entry.newsBlock.type('blockType').limit(1) %}
    {{ block.title }}
{% endfor %} 

ought to do it?
If not you could simply do what you're currently doing but also use a 
{% if loop.first %}
...
{% endif %}

inside it to only spit out content from the first loop iteration.


---

