1

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 %}
<article>
    <h4>{{ block.heading }}</h4>]
    <p>{{ block.bodyContent }}</p>
</article>
{% 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 %}
    <article>
        <p>{{ testimonialEntryMatrixBlocks.body }}</p>
        <p>{{ testimonialEntryMatrixBlocks.quote }}</p>
        <p>{{ testimonialEntryMatrixBlocks.role }}</p>
    </article>
    {% 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 %}
        <article>
            <p>{{ testimonial.body }}</p>
            <p>{{ testimonial.quote }}</p>
            <p>{{ testimonial.role }}</p>
        </article>
        {% endfor %}
    {% endif %}

1 Answer 1

0

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 %}
2
  • I've added what ive being using, im abit unsure how to go about using {% set %} though? Thank you
    – Spheriri
    Commented Mar 17, 2021 at 22:27
  • 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: craftcms.com/docs/3.x/dev/twig-primer.html or go straight to the official docs: 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. Commented Mar 18, 2021 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.