1

So I have a structure with a few entries and a single. I want to list out the first level of the structure, plus the single. Then somewhere else, list out the second level.

I've tried the following:

{% set myPages = craft.entries.section('structureName')|merge(craft.entries.section('singleName')) %}

<nav>
{% nav entry in myPages.level(1) %}
  {{ entry.title }}
{% endnav %}
<nav>

<aside>
{% for entry in myPages.level(2) %}
  {{ entry.title }}
{% endfor %}
<aside>

Which returns this error:

"Array to string conversion"

When I remove the .level(X) function it will return all the entries, on all levels; including the single.

For the purposes of making my problem more understandable, I've simplified my above example. I know I could just add the single entry directly into the <nav>. But I'm curious about ways to add to a entries section object without losing the ability to query levels.

So, in the same way I can exclude entries, is there a way to include an entry from a different section without relying on Twigs merge filter?

  • 1
    Have you tried level(1).first()? – Simon Müller Oct 12 '16 at 15:39
1

A colleague of mine shed some light on a very simple solution

{% set myPages = craft.entries.section(['structureName', 'singleName']).order('sectionId desc') %}

<nav>
{% nav entry in myPages.level([1, null]) %}
  {{ entry.title }}
{% endnav %}
<nav>

<aside>
{% for entry in myPages.level(2) %}
  {{ entry.title }}
{% endfor %}
<aside>

Passing an array of handles/slugs instead of just a string works perfectly. Since singles don't have levels; defining null in the level pulls them out too.

| improve this answer | |

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.