Adding a random entry to an array 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]) %}
  &lt;li&gt;{{ navi.title }}&lt;/li&gt;
{% 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]) %}
  &lt;li&gt;{{ navi.title }}&lt;/li&gt;
  {% if loop.index == '2' %}
    &lt;li&gt;{{ randEntry.title }}&lt;/li&gt;
  {% endif %}
{% endfor %}


But that's ugly. Is it possible to pass the whole thing into one array without conditional trickery?
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:]) %}