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

Pagination for findTaxonomy() results

Started by Muut Archive 10 years ago · 1 replies · 373 views
10 years ago

I'm using findTaxonomy() to show a collection based on a URI param (otherwise I'd use the page's frontmatter). It's working great except I can't seem to find a way to get pagination working to limit the results. Is this possible? Is there another recommended way to handle cases like this?

Sample code:

TWIG

{% set urlCategory = grav.uri.param('category') %}
{% set articlesInCategory = taxonomy.findTaxonomy({
    category: ('category.' ~ urlCategory)|t
}).order('default', 'asc') %}
---
10 years ago

As a "dirty fix" I have sometimes just done a manual pagination, like this:

TWIG
{% if uri.param('page') %}
    {% set index = uri.param('page') %}
{% else %}
    {% set index = 1 %}
    {% set start = 0 %}
    {% set stop = 5 %}
{% endif %}

{% if uri.param('page') %}
    {% set start = index * 5 - 5 %}
    {% set stop = index * 5 %}
{% endif %}

{% set index_len = page.children|length / 5 %}
{% set index_len = index_len|round(0, 'ceil') %}

It is based on a URI parameter, page being set - indicating what "page" of the pagination is shown, each page containing 5 items.

Thus start defines the start of the array (numerically) and stop the end of it. You can use these with Twig's slice() to produce the desired result.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1368 9 years ago
Archive · by Muut Archive, 9 years ago
2 940 9 years ago
Archive · by Muut Archive, 9 years ago
2 4069 9 years ago
Archive · by Muut Archive, 9 years ago
1 2960 9 years ago
Archive · by Muut Archive, 9 years ago
3 1125 9 years ago