<p>One way I have achieved this is like so:</p>

<pre><code>{% for entry in artists %}
  {% if loop.first %}
    &lt;div class="flex flex-wrap items-center"&gt;
  {% endif %}
  {% if loop.index == 5 %}
    &lt;/div&gt;
    &lt;div class="flex flex-wrap items-center lg:ml-20"&gt;
  {% endif %}
  &lt;figure class="w-1/4 sm:w-100px mb-0 border border-white"&gt;
    {% include '_includes/_components/image.twig' with {
      image: entry.artistImage[0],
      alt: 'Artists',
      transform: 'square',
      classes:'w-full',
      sizes:'(min-width:576px), 100px, 25vw',
      widthEnd: 800
    } %}
  &lt;/figure&gt;
  {% if loop.last %}
    &lt;/div&gt;
  {% endif %}
{% endfor %}
</code></pre>

<p>But I think this circumvents the issue rather than solves the question. </p>

CC BY-SA 4.0
2019-04-16T11:14:27.107Z
2019-04-16T11:14:27.107Z
Terry Upton
<p>So I want to get 9 random entries from a section and I do this like so:</p>

<pre><code>{% set artists = craft.entries
  .section('artists')
  .artistImage(':notempty:')
  .with(['artistImage'])
  .orderBy('RAND()')
  .limit(9) %}
</code></pre>

<p>Now I want to split these entries into a row of 4 and a row of 5. I am doing this like so:</p>

<pre><code>&lt;div class="flex flex-wrap items-center"&gt;
  {% for entry in artists|slice(0, 4) %}
    &lt;figure class="w-1/4 sm:w-100px mb-0 border border-white"&gt;
      {% include '_includes/_components/image.twig' with {
        image: entry.artistImage[0],
        alt: 'Artists',
        transform: 'square',
        classes:'w-full',
        sizes:'(min-width:576px), 100px, 25vw',
        widthEnd: 800
      } %}
      {{ entry.title }}
    &lt;/figure&gt;
  {% endfor %}
&lt;/div&gt;

&lt;div class="flex flex-wrap items-center lg:ml-20"&gt;
  {% for entry in artists|slice(4) %}
    &lt;figure class="w-1/5 sm:w-100px mb-0  border border-white"&gt;
      {% include '_includes/_components/image.twig' with {
        image: entry.artistImage[0],
        alt: 'Artists',
        transform: 'square',
        classes:'w-full',
        sizes:'(min-width:576px), 100px, 20vw',
        widthEnd: 800
      } %}
      {{ entry.title }}
    &lt;/figure&gt;
  {% endfor %}
</code></pre>

<p>However, this seems to randomise the each split and sometimes I get duplicate entries in each split.</p>

<p>Ideally, I want to get 9 random items and then keep this order and split this into to separate rows.</p>

<p>How can I achieve this? </p>

CC BY-SA 4.0
2019-04-16T11:24:48.320Z
if you use .all() on the query doesn't that set the order of the array returned so the slice will work on the array rather than the query (which is deprecated behaviour anyway right?)?
billythekid
CC BY-SA 4.0
2019-04-17T08:43:39.783Z
Yes you are correct @billythekid - I had missed off the .all() - still getting used to adding that.
Terry Upton
<templating><entries><array>
Get random entries from a section but keep the order of these when splitting the array
# Get random entries from a section but keep the order of these when splitting the array

(Asked by: username_0 on 2019-04-16)

So I want to get 9 random entries from a section and I do this like so:
{% set artists = craft.entries
  .section('artists')
  .artistImage(':notempty:')
  .with(['artistImage'])
  .orderBy('RAND()')
  .limit(9) %}

Now I want to split these entries into a row of 4 and a row of 5. I am doing this like so:

  {% for entry in artists|slice(0, 4) %}
    
      {% include '_includes/_components/image.twig' with {
        image: entry.artistImage[0],
        alt: 'Artists',
        transform: 'square',
        classes:'w-full',
        sizes:'(min-width:576px), 100px, 25vw',
        widthEnd: 800
      } %}
      {{ entry.title }}
    
  {% endfor %}



  {% for entry in artists|slice(4) %}
    
      {% include '_includes/_components/image.twig' with {
        image: entry.artistImage[0],
        alt: 'Artists',
        transform: 'square',
        classes:'w-full',
        sizes:'(min-width:576px), 100px, 20vw',
        widthEnd: 800
      } %}
      {{ entry.title }}
    
  {% endfor %}

However, this seems to randomise the each split and sometimes I get duplicate entries in each split.
Ideally, I want to get 9 random items and then keep this order and split this into to separate rows.
How can I achieve this?

**Tags:** templating, entries, array
> **Comments:**
> **username_1**: if you use .all() on the query doesn't that set the order of the array returned so the slice will work on the array rather than the query (which is deprecated behaviour anyway right?)?  
> **username_0**: Yes you are correct @billythekid - I had missed off the .all() - still getting used to adding that.  

---

## 1 answers

### Answer by username_0 on 2019-04-16. Score: 1

One way I have achieved this is like so:
{% for entry in artists %}
  {% if loop.first %}
    
  {% endif %}
  {% if loop.index == 5 %}
    
    
  {% endif %}
  
    {% include '_includes/_components/image.twig' with {
      image: entry.artistImage[0],
      alt: 'Artists',
      transform: 'square',
      classes:'w-full',
      sizes:'(min-width:576px), 100px, 25vw',
      widthEnd: 800
    } %}
  
  {% if loop.last %}
    
  {% endif %}
{% endfor %}

But I think this circumvents the issue rather than solves the question.


---

