4

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

11

If you need to get the level 2 children for a specific Structure entry, and you have that entry's EntryModel (i.e. you have an entry variable which refers to the Structure entry), you can just use the EntryModel's children property, appending level to only pull children from the desired level:

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

There's also a descendantOf parameter which can be used in a craft.entries query – obviously, this should be the Structure entry's ID (or the complete EntryModel, i.e. entry):

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

Also, if you want to pull entries at a certain level in a Structure section but not limit it to children of a certain entry, you can append the level parameter to your basic craft.entries query:

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

{% for entry in entries %}
    ...
{% endfor %}

Note that craft.entries queries can also be typed out using object notation, if you prefer (I find the object notation more readable, especially if you have a lot of parameters):

{% set entries = craft.entries({
    'section' : 'yourSectionHandle',
    'level' : 2
}) %}
  • Thanks for this, I didn't know how to use descendantOf. – mateostabio Jan 16 at 21:25

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.