<p>Try replacing</p>

<pre><code>{% for asset in block.imageField %}
    &lt;img src="{{asset.getUrl}}" alt="{{ asset.title }}" /&gt;
{% endfor %}
</code></pre>

<p>with</p>

<pre><code>{% set asset = block.imageField.first() %}
&lt;img src="{{asset.getUrl}}" alt="{{ asset.title }}" /&gt;
</code></pre>

<p>If you're using Craft 3, replace .first() with .one()</p>

CC BY-SA 4.0
2018-10-26T13:58:37.280Z
This makes sense and I thought for sure this would solve it but this still returns images for each block.
seth
CC BY-SA 4.0
2018-10-26T14:36:09.130Z
Are you sure it isn't an issue with your initial for loop? If I test your code as-is, the loop isn't limited to a single block, it's calling all of your videoPost blocks, so it's displaying all of the images associated with those blocks.
artmem
CC BY-SA 4.0
2018-10-26T15:08:32.473Z
Yes, that is the case. Thank you. I removed the initial for loop and used the following `{% if block.imageField | length %}{% set asset = block.imageField.first() %}<img src="{{ siteUrl | trim('/') }}{{asset.getUrl}}" alt="{{ asset.title }}" />{% endif %}`
seth
CC BY-SA 4.0
2018-10-25T22:26:38.743Z
2018-10-25T22:26:38.743Z
artmem
<p>I have a <code>videoPost</code> block in a Matrix field. Each <code>videoPost</code> has a <code>imageField</code>. I am trying to display the block’s image if <code>imageField</code> is defined. This is returning images for each block that has <code>imageField</code> defined when I only want to return the specific block’s image.</p>

<pre><code>{% for block in entry.matrixField.type('videoPost') %}
    {% if block.imageField | length %}
        {% for asset in block.imageField %}
            &lt;img src="{{asset.getUrl}}" alt="{{ asset.title }}" /&gt;
        {% endfor %}
    {% endif %}
{% endfor %}
</code></pre>

<matrix>
Display a Matrix field block entry when the block's entry exists
# Display a Matrix field block entry when the block's entry exists

(Asked by: username_0 on 2018-10-25)

I have a videoPost block in a Matrix field. Each videoPost has a imageField. I am trying to display the block’s image if imageField is defined. This is returning images for each block that has imageField defined when I only want to return the specific block’s image.
{% for block in entry.matrixField.type('videoPost') %}
    {% if block.imageField | length %}
        {% for asset in block.imageField %}
            
        {% endfor %}
    {% endif %}
{% endfor %}

**Tags:** matrix

---

## 1 answers

### Answer by username_1 on 2018-10-25. Score: 1 | Accepted answer

Try replacing
{% for asset in block.imageField %}
    
{% endfor %}

with
{% set asset = block.imageField.first() %}


If you're using Craft 3, replace .first() with .one()

> **Comments:**
> **username_0**: This makes sense and I thought for sure this would solve it but this still returns images for each block.  
> **username_1**: Are you sure it isn't an issue with your initial for loop? If I test your code as-is, the loop isn't limited to a single block, it's calling all of your videoPost blocks, so it's displaying all of the images associated with those blocks.  
> **username_0**: Yes, that is the case. Thank you. I removed the initial for loop and used the following `{% if block.imageField | length %}{% set asset = block.imageField.first() %}<img src="{{ siteUrl | trim('/') }}{{asset.getUrl}}" alt="{{ asset.title }}" />{% endif %}`  

---

