Have you seen our video tutorials? Check 'em out!

Display child items

huzzi - 2 months ago

I have the following content structure: -/ -/events -/events/events-1 -/events/events-2 -/events/events-3

I would like to know how would I list all 3 events in the /events page?

Couldn't find any docs on how to get child items.

Thanks

ryanthompson - 2 months ago

Is your event a stream? If so you can disable the "Require Exact URI Match" option for the event page and use the segment as an ID.

{% set event = entries('example', 'events').find(request_segments()|last) %}

That way you don't have multiple pages per event

Back to your question though; you can get a pages children like {{ pages.children }} which will return a page collection of the children. Same as you can do {{ page.parent }} the other way.

Hope this helps!

huzzi - 2 months ago

Thanks for your quick response. Sorry, I'm not sure what you mean if event a stream. Basically, I created two new page types one called "Events" and another page type called "Event Page" then create a page using "Events" page type at a root level then created 5 child pages under the Events page using "Event Page" page type.

Now I like to display all 5 child pages on "Events" page with the link and title so when you click on it, it takes you to the child page.

From your answer it looks like I should be able to use {{ pages.children }} on the Page Layout but don't know how to loop through the child items.

I've tried this code below but it doesn't output anything:

    {% for event in pages.children %}
  • {{ event.title }}
  • {% endfor %}

ryanthompson - 2 months ago

No problem! Usually you would want to make a separate stream for events in this case but there are plenty of ways to do things and your way is certainly one of them!

Note it should be page not pages like you have there. You're getting the child pages of the current page. You can also access the current page in the template super variable like this: {{ template.page.children }}.

If you are IN one of those child pages - you could still get the other children but you would need to go through the parent page (since you're in a child page). That would look like this: {{ page.parent.children }} or in the super variable: {{ template.page.parent.children }}.

Let me know if this works out for ya!

huzzi - 2 months ago

Thanks that worked nicely, creating steam probably the best way of doing it?