Get Next or Previous Collection Item Universal Filters Jump to heading
New in v0.11.0 Fetch the previous and next items in a collection when you pass in the current page
object.
Syntax Liquid
{% assign previousPost = collections.posts | getPreviousCollectionItem: page %}
{% assign nextPost = collections.posts | getNextCollectionItem: page %}
<!-- in 2.0 the page argument is optional -->
{% assign previousPost = collections.posts | getPreviousCollectionItem %}
{% assign nextPost = collections.posts | getNextCollectionItem %}
Syntax Nunjucks
{% set previousPost = collections.posts | getPreviousCollectionItem(page) %}
{% set nextPost = collections.posts | getNextCollectionItem(page) %}
<!-- in 2.0 the page argument is optional -->
{% set previousPost = collections.posts | getPreviousCollectionItem %}
{% set nextPost = collections.posts | getNextCollectionItem %}
This example has not yet been added. Do you want to contribute it? Edit this page
Useful when you’d like to link to the previous or next template in your collection:
Syntax Liquid
{% 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 %}
Syntax Nunjucks
{% 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. Do you want to contribute it? Edit this page
The Collections documentation outlines the default sorting algorithm and how to override it.
Also getCollectionItem
Jump to heading
For completeness, a getCollectionItem
filter is also included that fetches the current page from a collection.
Syntax Liquid
{% assign currentPost = collections.posts | getCollectionItem: page %}
<!-- in 2.0 the page argument is optional -->
{% assign currentPost = collections.posts | getCollectionItem %}
Syntax Nunjucks
{% 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. Do you want to contribute it? Edit this page