Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-07T07:22:40.310Z
Answers.LastActivityDate: 2019-03-07T07:22:40.310Z
Answers.UserDisplayName: Robin Schambach
Body: <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>
  
Tags: <templating><craft3><craft2>
Title: How can I return only 1 random block in a Matrix in Craft using Twig?
ThreadText: # 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?
  
  ```PHP
  {% if entry.carouselMatrix is not empty %}
      {% set currentSlide = shuffle(entry.carouselMatrix.slides).first() %}
      <div class="hero__wrapper">
          {% for slide in currentSlide.first() %}
              {% set image = slide.image.first() %}
              <div class="hero__image" style="background-image: url('{{ image.getUrl('heroHome') }}');"></div>
              <div class="hero__wrapper--inner grid">
                  <h1 class="hero__text grid__item desk-two-thirds">
                      {{ entry.heroText }}
                  </h1>
              </div>
              <div class="hero__image--caption">
                  <a href="{{ slide.linkUrl }}">
                      <span>{{ slide.caption }}</span>
                  </a>
              </div>
          {% endfor %}
      </div>
  {% 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](https://docs.craftcms.com/v3/dev/element-queries/matrix-block-queries.html#orderby) function.
  
  ```php
  {% 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 %}
  
  ```
  
  
  ---
  
  