Skip to content
● Grav 2.2 is out: 2x faster cold starts and 96% less memory on big sites. Read the announcement →

Get specific item in page collection

Started by Muut Archive 10 years ago · 1 replies · 514 views
10 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 %}

10 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 2558 9 years ago
Archive · by Muut Archive, 9 years ago
2 1687 9 years ago
Archive · by Muut Archive, 9 years ago
2 4977 9 years ago
Archive · by Muut Archive, 9 years ago
1 3801 9 years ago
Archive · by Muut Archive, 9 years ago
3 1797 9 years ago