8

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

Lindsey D
  • 23,974
  • 5
  • 53
  • 110
Design Frog
  • 133
  • 1
  • 1
  • 4

1 Answers1

23

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.

Mats Mikkel Rummelhoff
  • 22,361
  • 3
  • 38
  • 69