Skip to content
● Meet Grav Guard: keep every Grav site you run secure and up to date, all from one place. Free for up to 3 sites. Read more →

Access page children array by index

Started by Muut Archive 10 years ago · 1 replies · 606 views
10 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?

10 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 Muut Archive, 10 years ago
1 672 1 week ago
Archive · by Deleted User, 9 years ago
0 3042 9 years ago
Archive · by Muut Archive, 9 years ago
2 1983 9 years ago
Archive · by Muut Archive, 9 years ago
2 5336 9 years ago
Archive · by Muut Archive, 9 years ago
1 4134 9 years ago