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

Get specific item in page collection

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

I'd like to target specific items in a page collection.
e.g. I only want {{p.title}} from the first item, then the second in another part of the page, etc. How would I target a specific item?
Here's a snippet:
{% for p in page.collection %}
<h1 class="product-family">{{ p.title }}</h1>
{% endfor %}

9 years ago

There is several way to achieve this. with {{ loop.index }} you can get the current iteration, example:

TWIG
{% for p in page.collection %}
{% if loop.index == 1 %}
<h1 class=“product-family>{{ p.title }}</h1>
{% else %}
<h2>Do something else</h2>
{% endif %}
{% endfor %}

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1346 9 years ago
Archive · by Muut Archive, 9 years ago
2 933 9 years ago
Archive · by Muut Archive, 9 years ago
2 4060 9 years ago
Archive · by Muut Archive, 9 years ago
1 2945 9 years ago
Archive · by Muut Archive, 9 years ago
3 1117 9 years ago