Skip to content

Instantly share code, notes, and snippets.

@bransinanderson
Last active April 25, 2016 15:09
Show Gist options
  • Save bransinanderson/c29126953789d0ce9e34ce3507d6ad5d to your computer and use it in GitHub Desktop.
Save bransinanderson/c29126953789d0ce9e34ce3507d6ad5d to your computer and use it in GitHub Desktop.
Group page numbers in Craft pagination
{#
Group page numbers in Craft pagination
Example if we were on page 3 and numOfPaginatedItems is set to 5.
< 1 2 [3] 4 5 >
On page 7
< 6 [7] 8 9 10 >
On page 11
< [11] 12 13 14 15 >
#}
{% set numOfPaginatedItems = 5 %}
{% if pageInfo.nextUrl or pageInfo.prevUrl %}
{% if pageInfo.prevUrl %}
<a href="{{ pageInfo.prevUrl }}">
&larr; Previous
</a>
{% endif %}
<ul>
{# loop through the divisible number and the number of total pages #}
{% for i in range(1, pageInfo.totalPages, numOfPaginatedItems) %}
{# if current page is in range of divisible number #}
{% if pageInfo.currentPage in range(i, i + (numOfPaginatedItems - 1)) %}
{% set beginPage = i %}
{# beginning page number and increment it by numOfPaginatedItems #}
{% for page in beginPage..beginPage + (numOfPaginatedItems - 1) if page <= pageInfo.totalPages %}
<li{% if pageInfo.currentPage == page %} class="active"{% endif %}><a href="{{ pageInfo.getPageUrl(page) }}">{{ page }}</a></li>
{% endfor %}
{% endif %}
{% endfor %}
</ul>
{% if pageInfo.nextUrl %}
<a href="{{ pageInfo.nextUrl }}">
Next &rarr;
</a>
{% endif %}
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment