<p>You're close! The <code>relatedContent</code> field is an <a href="https://craftcms.com/docs/templating/elementcriteriamodel" rel="nofollow">ElementCriteriaModel</a>, so you need to loop over it (or otherwise treat it somewhat like an array) to get to the entries.</p>

<p>The matrix field is also an array like thing, and you need to loop over it to get at the blocks. <a href="https://craftcms.com/docs/matrix-fields#templating" rel="nofollow">See here</a></p>

<p>Here is what I would do (assuming the matrix field handle is <code>genericContent</code>:</p>

<pre><code>{% for oneRelatedContent in entry.relatedContent %}
  {% for block in oneRelatedContent.genericContent %}
    &lt;article class="t{% if loop.first %} t--tall{% endif %}"&gt;
      &lt;div class="content"&gt;
        &lt;span class="subtitle"&gt;{{ oneRelatedContent.sectionTag.first }}&lt;/span&gt;
        &lt;h1&gt;{{ oneRelatedContent.title }}&lt;/h1&gt;
        {{ copy }}
      &lt;/div&gt;
    &lt;/article&gt;
  {% endfor %}
{% endfor %}
</code></pre>

<p>You want to use <code>loop.index</code> to tell you which matrix block this is.
If you need to count the entries in <code>relatedContent</code>, you will need <code>loop.parent.loop.index</code> <a href="http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable" rel="nofollow">Loop variable</a></p>

CC BY-SA 3.0
2016-08-08T13:25:01.870Z
Thanks for the reply. I'm getting an error: `Variable "copy" does not exist` — I tried changing `{{ copy }}` to `{{ oneRelatedContent.copy }}` but that just outputs the title for some reason. The Matrix handle is `genericContent` and the Block Type is `copyMatrix` but changing `{% for block in oneRelatedContent.genericContent %}` to `{% for block in oneRelatedContent.copyMatrix %}` gives me this error: `Craft\EntryModel and its behaviors do not have a method or closure named "copyMatrix".`
Chrish
CC BY-SA 3.0
2016-08-08T13:35:43.947Z
Got it — `{{ block.copy }}` outputs what I'm looking for
Chrish
CC BY-SA 3.0
2016-08-05T17:49:02.293Z
2016-08-05T17:49:02.293Z
Marion Newlevant
<p>Stuck on a problem at the moment… trying to output a Rich Text field (copy) from an entry. The copy is contained in a matrix field (so: Entry -> Matrix Field -> Copy Field) but I can’t access it no matter what I try.</p>

<p>The Matrix Field Type is <code>genericContent</code>, the entry is <code>relatedContent</code> and the copy field is simply named <code>copy</code>.</p>

<p>Here's the code I currently have, I've tried various other options but none work.</p>

<pre><code>{% set entries = entry.relatedContent %}

{% for entry in entries %}
    {% switch entries|length %}
        {% case '1' %}
            one
        {% case '2' %}
            &lt;article class="t{% if loop.first %} t--tall{% endif %}"&gt;
                &lt;div class="content"&gt;
                    &lt;span class="subtitle"&gt;{{ entry.sectionTag.first }}&lt;/span&gt;
                    &lt;h1&gt;{{ entry.title }}&lt;/h1&gt;
                    &lt;p&gt;{{ entry.genericContent.copyMatrix.copy.first }}&lt;/p&gt;
                &lt;/div&gt;
            &lt;/article&gt;
            &lt;pre&gt;{{dump(entry.genericContent.copyMatrix.copy.first)}}&lt;/pre&gt;
            &lt;br&gt;
        {% case '3' %}
            three
        {% case '4' %}
            four
    {% endswitch %}
{% endfor %}
</code></pre>

<matrix><forloop>
Can't access data in Matrix field
# Can't access data in Matrix field

(Asked by: username_0 on 2016-08-05)

Stuck on a problem at the moment… trying to output a Rich Text field (copy) from an entry. The copy is contained in a matrix field (so: Entry -> Matrix Field -> Copy Field) but I can’t access it no matter what I try.
The Matrix Field Type is genericContent, the entry is relatedContent and the copy field is simply named copy.
Here's the code I currently have, I've tried various other options but none work.
{% set entries = entry.relatedContent %}

{% for entry in entries %}
    {% switch entries|length %}
        {% case '1' %}
            one
        {% case '2' %}
            
                
                    {{ entry.sectionTag.first }}
                    {{ entry.title }}
                    {{ entry.genericContent.copyMatrix.copy.first }}
                
            
            {{dump(entry.genericContent.copyMatrix.copy.first)}}
            
        {% case '3' %}
            three
        {% case '4' %}
            four
    {% endswitch %}
{% endfor %}

**Tags:** matrix, forloop


---

## 1 answers

### Answer by username_1 on 2016-08-05. Score: 2 | Accepted answer

You're close! The relatedContent field is an ElementCriteriaModel, so you need to loop over it (or otherwise treat it somewhat like an array) to get to the entries.
The matrix field is also an array like thing, and you need to loop over it to get at the blocks. See here
Here is what I would do (assuming the matrix field handle is genericContent:
{% for oneRelatedContent in entry.relatedContent %}
  {% for block in oneRelatedContent.genericContent %}
    
      
        {{ oneRelatedContent.sectionTag.first }}
        {{ oneRelatedContent.title }}
        {{ copy }}
      
    
  {% endfor %}
{% endfor %}

You want to use loop.index to tell you which matrix block this is.
If you need to count the entries in relatedContent, you will need loop.parent.loop.index Loop variable

> **Comments:**
> **username_0**: Thanks for the reply. I'm getting an error: `Variable "copy" does not exist` — I tried changing `{{ copy }}` to `{{ oneRelatedContent.copy }}` but that just outputs the title for some reason. The Matrix handle is `genericContent` and the Block Type is `copyMatrix` but changing `{% for block in oneRelatedContent.genericContent %}` to `{% for block in oneRelatedContent.copyMatrix %}` gives me this error: `Craft\EntryModel and its behaviors do not have a method or closure named "copyMatrix".`  
> **username_0**: Got it — `{{ block.copy }}` outputs what I'm looking for  

---

