3

There are several questions like this already but none seem to cover my use case. I have a structure that looks something like this:

Landing Page 1
- Case Studies
-- Case Study 1
-- Case Study 2
-- Case Study 3
- Sub page
- Sub page
Landing Page 2
- Case Studies
-- Case Study 1
-- Case Study 2
-- Case Study 3
- Sub page
- Sub page

If I'm in any child of the 'Landing Pages' I want to create a nav (as per the docs) but I cant' seem to get all of the pages I need. I can easily get the top level 'landing page':

entry.getAncestors().level(1).first()

I can then get the descendants separately:

entry.getAncestors().level(1).first().getDescendants()

However, I want a single ElementCriteriaModel with everything from the 'landing page' down in it.

Is this possible?

4

Untested, but you should be able to do this by using the merge filter in Twig to join the two results together.

Get the top level 'landing page' for this section:

{% set landingPage = entry.getAncestors().level(1) %}

Get the descendants for this section:

{% set childPages = entry.getAncestors().level(1).getDescendants() %}

Merge the two results together:

{% set sectionPages = [ landingPage ]|merge(childPages) %}

Now create the navigation for the section using the sectionPages variable:

<nav>
    <ul>
        {% nav page in sectionPages %}
            <li>
                {{ page.getLink() }}

                {% ifchildren %}
                    <ul>
                        {% children %}
                    </ul>
                {% endifchildren %}
            </li>
        {% endnav %}
    </ul>
</nav>
5
  • Thanks for answering! Merging throws an error "The merge filter only works with arrays or hashes; object and object given." Feb 5 '15 at 16:36
  • Ah, OK - I did say it was untested! It looks like you'll need to follow carlcs's answer and get the IDs of the entries first. Feb 5 '15 at 16:41
  • Just seen @carlcs edit to my answer - does that help? Feb 5 '15 at 16:41
  • @Steve should work now, yep. Working with IDs first, allows you to use an ElementCriteriaModel (with pagination etc.), something you can't do with an array of EntryModels. But in this case it's actually not necessary and I like this (more simple) answer better. Performance should be about the same, I think.
    – carlcs
    Feb 5 '15 at 16:45
  • I also prefer this answer, will mark this one instead. Feb 6 '15 at 8:59
4

Basically the same as Steve suggested, but I first merged the entries' IDs and then use them as a criteria in another craft.entries call:

{% set landing = entry.getAncestors().level(1).first() %}

{% set landingId = landing.id %}
{% set ancestorIds = landing.getDescendants().ids() %}
{% set entryIds = [ landingId ]|merge( ancestorIds ) %}

{% set entries = craft.entries.id(entryIds) %}
1
  • This works. Nice one! Feb 5 '15 at 16:37

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.