Menu
- Why Eleventy?
- Getting Started
- Community
- Working with Templates
- Using Data
- Configuration
- Template Languages
- Plugins
- API Services
- Release History
- Advanced
1.93s
29.05s
Get Next or Previous Collection Item Universal Filters
Contents
getPreviousCollectionItem
and getNextCollectionItem
Jump to heading
Fetch the previous and next items in a collection when you pass in the current page
object.
{% assign previousPost = collections.posts | getPreviousCollectionItem: page %}
{% assign nextPost = collections.posts | getNextCollectionItem: page %}
<!-- in v2.0.0-beta.1 the page argument is optional -->
{% assign previousPost = collections.posts | getPreviousCollectionItem %}
{% assign nextPost = collections.posts | getNextCollectionItem %}
{% set previousPost = collections.posts | getPreviousCollectionItem(page) %}
{% set nextPost = collections.posts | getNextCollectionItem(page) %}
<!-- in v2.0.0-beta.1 the page argument is optional -->
{% set previousPost = collections.posts | getPreviousCollectionItem %}
{% set nextPost = collections.posts | getNextCollectionItem %}
This example has not yet been added—you can swap to another template language above! Or maybe you want to contribute it? Edit this page(in latest version)
This example has not yet been added—you can swap to another template language above! Or maybe you want to contribute it? Edit this page(in latest version)
Useful when you’d like to link to the previous or next template in your collection:
{% if previousPost %}Previous Blog Post: <a href="{{ previousPost.url }}">{{ previousPost.data.title }}</a>{% endif %}
{% if nextPost %}Next Blog Post: <a href="{{ nextPost.url }}">{{ nextPost.data.title }}</a>{% endif %}
{% if previousPost %}Previous Blog Post: <a href="{{ previousPost.url }}">{{ previousPost.data.title }}</a>{% endif %}
{% if nextPost %}Next Blog Post: <a href="{{ nextPost.url }}">{{ nextPost.data.title }}</a>{% endif %}
This example has not yet been added—you can swap to another template language above! Or maybe you want to contribute it? Edit this page(in latest version)
This example has not yet been added—you can swap to another template language above! Or maybe you want to contribute it? Edit this page(in latest version)
The Collections documentation outlines the default sorting algorithm and how to override it.
getCollectionItemIndex
Added in v2.0.0-beta.1
Jump to heading
The getCollectionItemIndex
filter returns the 0-based numeric index of the current (or passed) page in the collection.
{% assign index = collections.posts | getCollectionItemIndex %}
Or pass it in:
{% assign index = collections.posts | getCollectionItemIndex: page %}
{% set index = collections.posts | getCollectionItemIndex %}
Or pass it in:
{% set index = collections.posts | getCollectionItemIndex(page) %}
This example has not yet been added—you can swap to another template language above! Or maybe you want to contribute it? Edit this page(in latest version)
This example has not yet been added—you can swap to another template language above! Or maybe you want to contribute it? Edit this page(in latest version)
getCollectionItem
Jump to heading
For completeness, a getCollectionItem
filter is also included that fetches the current page from a collection.
{% assign currentPost = collections.posts | getCollectionItem: page %}
<!-- in 2.0 the page argument is optional -->
{% assign currentPost = collections.posts | getCollectionItem %}
{% set currentPost = collections.posts | getCollectionItem(page) %}
<!-- in 2.0 the page argument is optional -->
{% set currentPost = collections.posts | getCollectionItem %}
This example has not yet been added—you can swap to another template language above! Or maybe you want to contribute it? Edit this page(in latest version)
This example has not yet been added—you can swap to another template language above! Or maybe you want to contribute it? Edit this page(in latest version)