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?

Mark Notton
  • 2,337
  • 1
  • 15
  • 30

1 Answers1

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.

Mark Notton
  • 2,337
  • 1
  • 15
  • 30