7

I'm new in the Craft CMS world. Today I start so build my first website with Craft.

I have a Structure with 2 Levels. Now I want so get the children from level 2. The level 1 entries are overview-pages for the content in the level 2 pages. I've build 2 entry types. One for the content and one overview pages.

Thx for your help :) Alexander

1 Answer 1

22

If you need to get the level 2 children for a specific parent entry, and you have that entry's entry model instance (i.e. you have an entry variable, referring to the parent entry), you can use that entry model's children property (which returns a pre-baked entry query, fetching that entry's child entries only). Add the level parameter to only fetch children at the desired level:

{% set children = entry.children().level(2).all() %}

For "standalone" entry queries, there's also the descendantOf parameter. This should be set to the parent entry's ID (you can also pass an entry model, e.g. entry):

{% set entries = craft.entries
    .section('yourSectionHandle')
    .descendantOf(entry.id)
    .level(2)
    .all()
%}

If you want to fetch entries from a structure section at a certain level, but not limited to children of any particular parent entry, you can append the level parameter to your basic entry query:

{% set entries = craft.entries.section('yourSectionHandle').level(2).all() %}

Note that level parameter doesn't necessarily have to be an integer; if you want to fetch entries at any level at or above 2, you can pass a string prefixed with the >= operator:

{% set entries = craft.entries.section('yourSectionHandle').level('>= 2').all() %}

Check out the official docs for the entry query level parameter for more examples.

1
  • Thanks for this, I didn't know how to use descendantOf. Jan 16, 2019 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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