<p>You can create a random order by including <code>rand()</code> in your <a href="https://docs.craftcms.com/v3/dev/element-queries/matrix-block-queries.html#orderby" rel="nofollow noreferrer">OrderBy</a> function.</p>

<pre class="lang-php prettyprint-override"><code>{% set randomBlock = entry.carouselMatrix.orderBy('rand()').one() %}
{% if randomBlock is not null %}
    {% set randomSlide = randomBlock.slides.orderBy('rand()').one() %}
    {% if randomSlide is not null %}
        {{ randomSlide .caption }}
        {% set image = randomSlide.image.one() %}
        {{ image is not null? image.getUrl() : '' }}
    {% endif %}
{% endif %}
</code></pre>

CC BY-SA 4.0
2019-03-07T07:22:40.310Z
2019-03-07T07:22:40.310Z
Robin Schambach
<p>I have a <code>matrix</code> with a couple of <code>text fields</code> for a caption, location and URL and an image</p>

<p>Every time this page loads I want a random slide to show - so the background image is random, as well as the relevant text fields.</p>

<p>I've tried several variations of the below but keep getting a template error of "Array to string conversion"</p>

<p>Why is this happening? How can I achieve the desired result?</p>

<pre class="lang-PHP prettyprint-override"><code>{% if entry.carouselMatrix is not empty %}
    {% set currentSlide = shuffle(entry.carouselMatrix.slides).first() %}
    &lt;div class="hero__wrapper"&gt;
        {% for slide in currentSlide.first() %}
            {% set image = slide.image.first() %}
            &lt;div class="hero__image" style="background-image: url('{{ image.getUrl('heroHome') }}');"&gt;&lt;/div&gt;
            &lt;div class="hero__wrapper--inner grid"&gt;
                &lt;h1 class="hero__text grid__item desk-two-thirds"&gt;
                    {{ entry.heroText }}
                &lt;/h1&gt;
            &lt;/div&gt;
            &lt;div class="hero__image--caption"&gt;
                &lt;a href="{{ slide.linkUrl }}"&gt;
                    &lt;span&gt;{{ slide.caption }}&lt;/span&gt;
                &lt;/a&gt;
            &lt;/div&gt;
        {% endfor %}
    &lt;/div&gt;
{% endif %}
</code></pre>

<templating><craft3><craft2>
How can I return only 1 random block in a Matrix in Craft using Twig?
# How can I return only 1 random block in a Matrix in Craft using Twig?

(Asked by: username_0 on 2019-03-07)

I have a matrix with a couple of text fields for a caption, location and URL and an image
Every time this page loads I want a random slide to show - so the background image is random, as well as the relevant text fields.
I've tried several variations of the below but keep getting a template error of "Array to string conversion"
Why is this happening? How can I achieve the desired result?
{% if entry.carouselMatrix is not empty %}
    {% set currentSlide = shuffle(entry.carouselMatrix.slides).first() %}
    
        {% for slide in currentSlide.first() %}
            {% set image = slide.image.first() %}
            
            
                
                    {{ entry.heroText }}
                
            
            
                
                    {{ slide.caption }}
                
            
        {% endfor %}
    
{% endif %}

**Tags:** templating, craft3, craft2


---

## 1 answers

### Answer by username_1 on 2019-03-07. Score: 4 | Accepted answer

You can create a random order by including rand() in your OrderBy function.
{% set randomBlock = entry.carouselMatrix.orderBy('rand()').one() %}
{% if randomBlock is not null %}
    {% set randomSlide = randomBlock.slides.orderBy('rand()').one() %}
    {% if randomSlide is not null %}
        {{ randomSlide .caption }}
        {% set image = randomSlide.image.one() %}
        {{ image is not null? image.getUrl() : '' }}
    {% endif %}
{% endif %}


---

