<p>There's a couple of problems with your current code – for one, you're (as Brad mentions in the comments above) doing some variable overwriting by reusing the <code>entry</code> and <code>block</code> names. Also, you're attempting to do a <code>for</code> loop on the result from a call to <code>first()</code>, which won't work (<code>first()</code> returns a single element, not an array).</p>

<p>In any case, I'd suggest getting rid of all the <code>for</code> loops (you're looping on a resultset with a single element in each of your inner loops), in an attempt to make the code a tad more clear.</p>

<p>Something like this should work:</p>

<pre><code>{% for entry in craft.entries.section('sectionHandle') %}
    {% set outerBlock = entry.matrixHandle.first() %}
    {% set outerBlockEntry = outerBlock ? outerBlock.entryFieldMatrixHandle.first() %}
    {% set innerBlock = outerBlockEntry ? outerBlockEntry.entrysMatrixHandle.first() %}
    {{ innerBlock ? innerBlock.fieldHandle }}
{% endfor %}
</code></pre>

<p>That's a lot of <code>{% set %}</code> statements, though – you could also do this (although I'm honestly not sure which option is more readable):</p>

<pre><code>{% for entry in craft.entries.section('sectionHandle') %}
    {% set block = entry.matrixHandle.first().entryFieldMatrixHandle.first().entrysMatrixHandle.first()|default %}
    {{ block ? block.fieldHandle }}
{% endfor %}
</code></pre>

<p>Finally, as an alternative approach altogether you could use the <a href="https://craftcms.com/docs/relations#the-relatedTo-param" rel="nofollow"><code>relatedTo</code></a> param:</p>

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

    {% set block = craft.entries.relatedTo({
        sourceElement: entry.matrixHandle.first(),
        field: 'entryFieldMatrixHandle'
    }).first().entrysMatrixHandle.first()|default %}

    {{ block ? block.fieldHandle }}

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

<p>None of the above examples are tested, and now my head hurts – but hopefully it'll prove helpful :)</p>

CC BY-SA 3.0
2016-09-21T11:10:17.497Z
Thanks! I didn't realize the variable name could be change at the beginning of the for loop. I thought it was stuck as "block" and "entry". All set now!
Chattervast
CC BY-SA 3.0
2016-09-16T19:35:15.740Z
2016-09-16T19:42:52.620Z
2016-09-16T19:42:52.620Z
Mats Mikkel Rummelhoff
<p>Question is similar to some others I've looked through but their solutions don't work as there are some differences.</p>

<p>Currently this doesn't output any of the fields I try to access within the internal matrix.</p>

<pre><code>{% for entry in craft.entries.section('sectionHandle').find() %}
    {% for block in entry.matrixHandle.limit(1) %}
        {% for entry in block.entryFieldMatrixHandle.first() %}
            {% for block in entry.entrysMatrixHandle.limit(1) %}
                {{ block.fieldHandle }}
            {% endfor %}
        {% endfor %}
    {% endfor %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2016-09-16T17:35:28.160Z
Not sure if it will solve your problem or not, but your inner `{% for entry` and `{% for block` loops are overwriting the output `entry` and `block` variables that are defined in the outer loops. You'll want to rename them to something else.
Brad Bell
CC BY-SA 3.0
2016-09-21T11:10:32.930Z
Yep! Thanks! Didn't realize that's how it worked!
Chattervast
<matrix><entry><loop><blocks>
Display text field from Matrix of an Entry Field in Matrix
# Display text field from Matrix of an Entry Field in Matrix

(Asked by: username_0 on 2016-09-15)

Question is similar to some others I've looked through but their solutions don't work as there are some differences.
Currently this doesn't output any of the fields I try to access within the internal matrix.
{% for entry in craft.entries.section('sectionHandle').find() %}
    {% for block in entry.matrixHandle.limit(1) %}
        {% for entry in block.entryFieldMatrixHandle.first() %}
            {% for block in entry.entrysMatrixHandle.limit(1) %}
                {{ block.fieldHandle }}
            {% endfor %}
        {% endfor %}
    {% endfor %}
{% endfor %}

**Tags:** matrix, entry, loop, blocks
> **Comments:**
> **username_1**: Not sure if it will solve your problem or not, but your inner `{% for entry` and `{% for block` loops are overwriting the output `entry` and `block` variables that are defined in the outer loops. You'll want to rename them to something else.  
> **username_0**: Yep! Thanks! Didn't realize that's how it worked!  

---

## 1 answers

### Answer by username_2 on 2016-09-16. Score: 1 | Accepted answer

There's a couple of problems with your current code – for one, you're (as Brad mentions in the comments above) doing some variable overwriting by reusing the entry and block names. Also, you're attempting to do a for loop on the result from a call to first(), which won't work (first() returns a single element, not an array).
In any case, I'd suggest getting rid of all the for loops (you're looping on a resultset with a single element in each of your inner loops), in an attempt to make the code a tad more clear.
Something like this should work:
{% for entry in craft.entries.section('sectionHandle') %}
    {% set outerBlock = entry.matrixHandle.first() %}
    {% set outerBlockEntry = outerBlock ? outerBlock.entryFieldMatrixHandle.first() %}
    {% set innerBlock = outerBlockEntry ? outerBlockEntry.entrysMatrixHandle.first() %}
    {{ innerBlock ? innerBlock.fieldHandle }}
{% endfor %}

That's a lot of {% set %} statements, though – you could also do this (although I'm honestly not sure which option is more readable):
{% for entry in craft.entries.section('sectionHandle') %}
    {% set block = entry.matrixHandle.first().entryFieldMatrixHandle.first().entrysMatrixHandle.first()|default %}
    {{ block ? block.fieldHandle }}
{% endfor %}

Finally, as an alternative approach altogether you could use the relatedTo param:
{% for entry in craft.entries.section('sectionHandle') %}

    {% set block = craft.entries.relatedTo({
        sourceElement: entry.matrixHandle.first(),
        field: 'entryFieldMatrixHandle'
    }).first().entrysMatrixHandle.first()|default %}

    {{ block ? block.fieldHandle }}

{% endfor %}

None of the above examples are tested, and now my head hurts – but hopefully it'll prove helpful :)

> **Comments:**
> **username_0**: Thanks! I didn't realize the variable name could be change at the beginning of the for loop. I thought it was stuck as "block" and "entry". All set now!  

---

