<p>No need for a loop if you're just fetching a single entry:</p>
<pre><code>{% set testimonialEntry = craft.entries.section('testimonalEntries').orderBy('RAND()').limit(1).one() %}
{% set testimonialEntryMatrixBlocks = testimonialEntry.matrixField.all() %}
</code></pre>
<p>Ideally for better performance you'd also use <a href="https://craftcms.com/docs/3.x/dev/eager-loading-elements.html" rel="nofollow noreferrer">eager loading</a> to load the matrix blocks at the same time as fetching the entry:</p>
<pre><code>{% set testimonialEntry = craft.entries.section('testimonalEntries').orderBy('RAND()').limit(1).with(['matrixField']).one() %}
{% set testimonialEntryMatrixBlocks = testimonialEntry.matrixField %}
</code></pre>

CC BY-SA 4.0
2021-03-17T22:27:42.917Z
I've added what ive being using, im abit unsure how to go about using {% set %} though? Thank you
Spheriri
CC BY-SA 4.0
2021-03-18T08:31:13.817Z
If you've never used `{% set %}` before, I'd recommend that you spend some time getting familiar with the basics of Twig, otherwise you're going to run into a lot of frustration! The Craft docs give a quick primer: https://craftcms.com/docs/3.x/dev/twig-primer.html or go straight to the official docs: https://twig.symfony.com/doc/3.x - You're also going to need to understand the difference between arrays and strings for a better understanding of how to code in Twig.
James Smith
CC BY-SA 4.0
2021-03-17T22:12:59.470Z
2021-03-17T22:12:59.470Z
James Smith
<p>I know im doing something small wrong but I cant see what at this point.</p>
<p>I have a channel called 'testimonialEntries' and I want to randomly output one of these entries on seperate pages. I cant output the matrix block in 'testimonialEntries' but i can the title, so i know its working just not accessing the matrix block.</p>
<pre><code>{% for entries in craft.entries.section('testimonalEntries').orderBy('RAND()').limit(1).all() %}
{% block in entry.matrixBlock %}
&lt;article&gt;
    &lt;h4&gt;{{ block.heading }}&lt;/h4&gt;]
    &lt;p&gt;{{ block.bodyContent }}&lt;/p&gt;
&lt;/article&gt;
{% endfor %}{% endfor %}
</code></pre>
<p>In this example im getting the entry title, but how do i also get a matrix block?</p>
<p>Edit: AM I getting close?</p>
<pre><code>{% set testimonialEntry = craft.entries.section('testimonalEntries').orderBy('RAND()').limit(1).with(['testimonial']).one() %}
{% set testimonialEntryMatrixBlocks = testimonialEntry.testimonial %}
    {% if testimonialEntryMatrixBlocks %}
    &lt;article&gt;
        &lt;p&gt;{{ testimonialEntryMatrixBlocks.body }}&lt;/p&gt;
        &lt;p&gt;{{ testimonialEntryMatrixBlocks.quote }}&lt;/p&gt;
        &lt;p&gt;{{ testimonialEntryMatrixBlocks.role }}&lt;/p&gt;
    &lt;/article&gt;
    {% endif %}
</code></pre>
<p>Ok final edit incase anyone else runs across this: It Works!</p>
<pre><code>    {% set testimonialEntry = craft.entries.section('testimonalEntries').orderBy('RAND()').limit(1).with(['testimonial']).one() %}
{% set testimonialEntryMatrixBlocks = testimonialEntry.testimonial %}
    {% if testimonialEntryMatrixBlocks %}
        {% for testimonial in testimonialEntryMatrixBlocks %}
        &lt;article&gt;
            &lt;p&gt;{{ testimonial.body }}&lt;/p&gt;
            &lt;p&gt;{{ testimonial.quote }}&lt;/p&gt;
            &lt;p&gt;{{ testimonial.role }}&lt;/p&gt;
        &lt;/article&gt;
        {% endfor %}
    {% endif %}
</code></pre>

<templating><entries><matrix>
Output Channel entries on another page with a matrix block
# Output Channel entries on another page with a matrix block

(Asked by: username_0 on 2021-03-17)

I know im doing something small wrong but I cant see what at this point.
I have a channel called 'testimonialEntries' and I want to randomly output one of these entries on seperate pages. I cant output the matrix block in 'testimonialEntries' but i can the title, so i know its working just not accessing the matrix block.
{% for entries in craft.entries.section('testimonalEntries').orderBy('RAND()').limit(1).all() %}
{% block in entry.matrixBlock %}

    {{ block.heading }}]
    {{ block.bodyContent }}

{% endfor %}{% endfor %}

In this example im getting the entry title, but how do i also get a matrix block?
Edit: AM I getting close?
{% set testimonialEntry = craft.entries.section('testimonalEntries').orderBy('RAND()').limit(1).with(['testimonial']).one() %}
{% set testimonialEntryMatrixBlocks = testimonialEntry.testimonial %}
    {% if testimonialEntryMatrixBlocks %}
    
        {{ testimonialEntryMatrixBlocks.body }}
        {{ testimonialEntryMatrixBlocks.quote }}
        {{ testimonialEntryMatrixBlocks.role }}
    
    {% endif %}

Ok final edit incase anyone else runs across this: It Works!
    {% set testimonialEntry = craft.entries.section('testimonalEntries').orderBy('RAND()').limit(1).with(['testimonial']).one() %}
{% set testimonialEntryMatrixBlocks = testimonialEntry.testimonial %}
    {% if testimonialEntryMatrixBlocks %}
        {% for testimonial in testimonialEntryMatrixBlocks %}
        
            {{ testimonial.body }}
            {{ testimonial.quote }}
            {{ testimonial.role }}
        
        {% endfor %}
    {% endif %}

**Tags:** templating, entries, matrix


---

## 1 answers

### Answer by username_1 on 2021-03-17. Score: 0 | Accepted answer

No need for a loop if you're just fetching a single entry:
{% set testimonialEntry = craft.entries.section('testimonalEntries').orderBy('RAND()').limit(1).one() %}
{% set testimonialEntryMatrixBlocks = testimonialEntry.matrixField.all() %}

Ideally for better performance you'd also use eager loading to load the matrix blocks at the same time as fetching the entry:
{% set testimonialEntry = craft.entries.section('testimonalEntries').orderBy('RAND()').limit(1).with(['matrixField']).one() %}
{% set testimonialEntryMatrixBlocks = testimonialEntry.matrixField %}

> **Comments:**
> **username_0**: I've added what ive being using, im abit unsure how to go about using {% set %} though? Thank you  
> **username_1**: If you've never used `{% set %}` before, I'd recommend that you spend some time getting familiar with the basics of Twig, otherwise you're going to run into a lot of frustration! The Craft docs give a quick primer: https://craftcms.com/docs/3.x/dev/twig-primer.html or go straight to the official docs: https://twig.symfony.com/doc/3.x - You're also going to need to understand the difference between arrays and strings for a better understanding of how to code in Twig.  

---

