2

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]) %}
  <li>{{ navi.title }}</li>
{% 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]) %}
  <li>{{ navi.title }}</li>
  {% if loop.index == '2' %}
    <li>{{ randEntry.title }}</li>
  {% endif %}
{% endfor %}

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

3

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:]) %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.