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

Get specific item in page collection

Started by Muut Archive 10 years ago · 1 replies · 476 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 2287 9 years ago
Archive · by Muut Archive, 9 years ago
2 1500 9 years ago
Archive · by Muut Archive, 9 years ago
2 4750 9 years ago
Archive · by Muut Archive, 9 years ago
1 3565 9 years ago
Archive · by Muut Archive, 9 years ago
3 1624 9 years ago