Skip to content
Grav 2.1 is out: every page speaks Markdown. Read the announcement →

Access page children array by index

Started by Muut Archive 9 years ago · 1 replies · 515 views
9 years ago

I have an array of pages that can be accessed by using a for loop. Somehow trying to access elements by index is not working.

TWIG
{% set posts = page.find('/blog').children.order('date', 'desc').slice(0, 3) %}
{% for post in posts %}
   {{ post.url }} // this works fine
{% endfor %}
{{ posts[0].url }} // this does not work

Any suggestion on what I am doing wrong?

9 years ago

You can only access by index within a loop, elsewhere the index does not exist. If you are trying to access the first item of an array, use the |first filter like so: {{ [1, 2, 3, 4]|first }}.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 2275 9 years ago
Archive · by Muut Archive, 9 years ago
2 1496 9 years ago
Archive · by Muut Archive, 9 years ago
2 4743 9 years ago
Archive · by Muut Archive, 9 years ago
1 3553 9 years ago
Archive · by Muut Archive, 9 years ago
3 1618 9 years ago