Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {#
- Display a pagination for entries instead of pages
- to display on entry detail pages.
- This will behave like this:
- Post 3
- Post 4
- Post 5
- Post 6 * current
- Post 7
- Post 8
- Post 9
- #}
- {% set paginationOrder = 'postDate desc' %}
- {# Set offset for previous entries to display the max. amount of entries #}
- {% set prevOffset = 4 %}
- {# Get all IDs #}
- {% set everyId = craft.entries.section('posts').limit(null).orderBy(paginationOrder).ids() %}
- {# Get current ID #}
- {% set currentIndex = everyId | indexOf(entry.id) %}
- {#
- Set offset to display 10 entries at the time sand
- keep current entry in the middle of list.
- #}
- {% if currentIndex == 0 %}
- {% set nextOffset = 8 %}
- {% elseif currentIndex == 1 %}
- {% set nextOffset = 7 %}
- {% elseif currentIndex == 2 %}
- {% set nextOffset = 6 %}
- {% elseif currentIndex == 3 %}
- {% set nextOffset = 5 %}
- {% else %}
- {% set nextOffset = 4 %}
- {% endif %}
- <ul>
- {# Get all previous entries #}
- {% if currentIndex != -1 %}
- {% set prevIds = everyId %}
- {% for item in craft.entries.id(prevIds).orderBy(paginationOrder).offset(currentIndex - prevOffset).all() if item.postDate|date('U') > entry.postDate|date('U') %}
- <li>
- <a href="{{item.url}}">{{item.title}}</a>
- </li>
- {% endfor %}
- {% endif %}
- {# Current entry #}
- <li>
- <strong>{{entry.title}}</strong>
- </li>
- {# Get all next entries #}
- {% if currentIndex != -1 %}
- {# Get next ID's and offset #}
- {% set nextIds = everyId | slice(currentIndex+1, nextOffset) %}
- {% set nextEntries = craft.entries.id(nextIds).fixedOrder(true) %}
- {% for nextEntry in nextEntries.all() %}
- <li>
- <a href="{{nextEntry.url}}">{{ nextEntry.title }}</a>
- </li>
- {% endfor %}
- {% endif %}
- </ul>
Add Comment
Please, Sign In to add comment