Skip to content

Instantly share code, notes, and snippets.

@cole007
Created December 15, 2015 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cole007/8fbe1247791985aea3e9 to your computer and use it in GitHub Desktop.
Save cole007/8fbe1247791985aea3e9 to your computer and use it in GitHub Desktop.
Paginate entries in craft by date
<!-- if no category set one as null -->
{% if category is not defined %}
{% set category = '' %}
{% endif %}
{% set date = now ~ ' 00:00:00' %}
<!-- this logic is shared across several locales so Section is defined externally to refer to the locale in question -->
{% set ids = craft.entries.section('event' ~ Section).eventDate('>= ' ~ date).relatedTo(category).limit(null).ids() %}
{% set page = 0 %}
{% if ids | length %}
{% set start = craft.request.pageNum - 1 %}
{% endif %}
{% set q = '' %}
{% if query is defined %}
{% set q = '?q=' ~ query %}
{% endif %}
<!-- create entries object -->
{% set entries = craft.entries.id(ids).order('eventDate asc') %}
<!-- set today -->
{% set start = now|date("Y-m-d") %}
<!-- set empty arrays -->
{% set days = [] %}
{% set ids = [] %}
<!-- work out bottom/first event date for all entries -->
{% set floor = entries.order('eventDate asc').first().eventDate|date('U') / 86400 %}
<!-- work out top/last event date for all entries -->
{% set ceil = entries.order('eventEnd desc').first().eventEnd|date('U') / 86400 %}
<!-- loop through all days between floor and ceil -->
{% for i in floor|round(0,'floor')..ceil|round(0,'ceil') %}
<!-- day as unix timestamp -->
{% set day = i*86400 %}
{% set start = day|date('Y-m-d') ~ ' 00:00:00' %}
{% set end = day|date('Y-m-d') ~ ' 23:59:59' %}
{% set spread = ['and', '>= ' ~ start, '< ' ~ end] %}
{% set search = craft.entries.section('event' ~ Section).relatedTo(category).eventDate(spread).ids() %}
{% set searchSpan = craft.entries.section('event' ~ Section).relatedTo(category).eventDate('<= ' ~ end).eventEnd('>= ' ~ start).ids() %}
{% set search = search|merge(searchSpan) %}
{% if search | length %}
{% set days = days|merge([day]) %}
{% set ids = ids|merge([search]) %}
{% endif %}
{% endfor %}
<!-- get range of 10 days based on current page -->
{% set daysSlice = days|slice(page,10) %}
<!-- loop through days -->
{% for id, day in daysSlice %}
<li class="list__item panel">
<time class="heading-3 h-heading" datetime="{{ day|date('Y-m-d') }}">{{ day | date('l j F') }}</time>
{% for event in entries.id(ids[id + page]) %}
<p>{{ event.title }}</p>
{% endfor %}
</li>
{% endfor %}
<!-- do pagination here -->
{% if days | length > 10 %}
{% set total = days | length / 10 %}
{% set total = total | round(0,'ceil') %}
<ul role="navigation" class="nav pagination__nav">
{% if page > 0 %}
{% set prev = craft.request.pageNum - 1 %}
<li class="pagination__item pagination__item--first"><a href="{{ craft.request.url ~ '/' ~ q}}">First</a></li>
<li class="pagination__item pagination__item--prev"><a href="{{ craft.request.url ~ '/p' ~ prev ~ q }}"></a></li>
{% endif %}
{% for i in 1..total %}
<li class="pagination__item pagination__item--count{% if craft.request.pageNum == i %} pagination__item--current{% endif %}"><a href="{{ craft.request.url ~ '/p' ~ i ~ q }}">{{ i }}</a></li>
{% endfor %}
{% if craft.request.pageNum < total %}
{% set next = craft.request.pageNum + 1 %}
<li class="pagination__item pagination__item--next"><a href="{{ craft.request.url ~ '/p' ~ next ~ q }}{{ q }}"></a></li>
<li class="pagination__item pagination__item--last"><a href="{{ craft.request.url ~ '/p' ~ total ~ q }}">Last</a></li>
{% endif %}
</ul>
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment