Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Access page children array by index

Started by Muut Archive 9 years ago · 1 replies · 430 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 1329 9 years ago
Archive · by Muut Archive, 9 years ago
2 922 9 years ago
Archive · by Muut Archive, 9 years ago
2 4051 9 years ago
Archive · by Muut Archive, 9 years ago
1 2930 9 years ago
Archive · by Muut Archive, 9 years ago
3 1108 9 years ago