<p>You can use <code>slice</code> and <code>merge</code> filters to “insert” that random entry into your array of entries.</p>

<p>In the following example I’m using the <code>id</code> param to query for the selected entries and I’m also setting <code>fixedOrder</code>, as this returns the entries in the same order as the IDs entered into the <code>id</code> param.</p>

<pre><code>{% set selectEntries = craft.entries.id('7, 2, 83, 9, 90').fixedOrder(true) %}
{% set randEntry = craft.entries.section('channelX').order('RAND()').first() %}

{% set navEntries = selectEntries[:2]|merge([randEntry])|merge(selectEntries[2:]) %}
</code></pre>

CC BY-SA 3.0
2016-07-04T17:04:12.220Z
2016-07-04T17:04:12.220Z
carlcs
<p>I have a navi on which one entry consists of a random channel entry from one section among standard singles:</p>

<ul>
<li>Single 1</li>
<li>Single 2</li>
<li>Random Channel entry</li>
<li>Single 3</li>
</ul>

<p>I thought about something like this:</p>

<pre><code>{% set randEntry = craft.entries.section('channelX').order('RAND()').first() %}
{% for navi in craft.entries.section(['single1', 'single2', randPortrait]) %}
  &lt;li&gt;{{ navi.title }}&lt;/li&gt;
{% endfor %}
</code></pre>

<p>This does not work. (no error, but the variable is simply ignored).</p>

<p>I found a solution to pass in the variable when looping trough the array</p>

<pre><code>{% set randEntry = craft.entries.section('channelX').order('RAND()').first() %}
{% for navi in craft.entries.section(['single1', 'single2', randPortrait]) %}
  &lt;li&gt;{{ navi.title }}&lt;/li&gt;
  {% if loop.index == '2' %}
    &lt;li&gt;{{ randEntry.title }}&lt;/li&gt;
  {% endif %}
{% endfor %}
</code></pre>

<p>But that's ugly. Is it possible to pass the whole thing into one array without conditional trickery?</p>

<navigation><variable><array><random>
Adding a random entry to an array
# Adding a random entry to an array

(Asked by: username_0 on 2016-07-04)

I have a navi on which one entry consists of a random channel entry from one section among standard singles:

Single 1
Single 2
Random Channel entry
Single 3

I thought about something like this:
{% set randEntry = craft.entries.section('channelX').order('RAND()').first() %}
{% for navi in craft.entries.section(['single1', 'single2', randPortrait]) %}
  {{ navi.title }}
{% endfor %}

This does not work. (no error, but the variable is simply ignored).
I found a solution to pass in the variable when looping trough the array
{% set randEntry = craft.entries.section('channelX').order('RAND()').first() %}
{% for navi in craft.entries.section(['single1', 'single2', randPortrait]) %}
  {{ navi.title }}
  {% if loop.index == '2' %}
    {{ randEntry.title }}
  {% endif %}
{% endfor %}

But that's ugly. Is it possible to pass the whole thing into one array without conditional trickery?

**Tags:** navigation, variable, array, random


---

## 1 answers

### Answer by username_1 on 2016-07-04. Score: 3 | Accepted answer

You can use slice and merge filters to “insert” that random entry into your array of entries.
In the following example I’m using the id param to query for the selected entries and I’m also setting fixedOrder, as this returns the entries in the same order as the IDs entered into the id param.
{% set selectEntries = craft.entries.id('7, 2, 83, 9, 90').fixedOrder(true) %}
{% set randEntry = craft.entries.section('channelX').order('RAND()').first() %}

{% set navEntries = selectEntries[:2]|merge([randEntry])|merge(selectEntries[2:]) %}


---

