Guest User

Untitled

a guest
Jun 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. {#
  2. Display a pagination for entries instead of pages
  3. to display on entry detail pages.
  4.  
  5. This will behave like this:
  6.  
  7. Post 3
  8. Post 4
  9. Post 5
  10. Post 6 * current
  11. Post 7
  12. Post 8
  13. Post 9
  14. #}
  15. {% set paginationOrder = 'postDate desc' %}
  16.  
  17. {# Set offset for previous entries to display the max. amount of entries #}
  18. {% set prevOffset = 4 %}
  19.  
  20. {# Get all IDs #}
  21. {% set everyId = craft.entries.section('posts').limit(null).orderBy(paginationOrder).ids() %}
  22.  
  23. {# Get current ID #}
  24. {% set currentIndex = everyId | indexOf(entry.id) %}
  25.  
  26. {#
  27. Set offset to display 10 entries at the time sand
  28. keep current entry in the middle of list.
  29. #}
  30. {% if currentIndex == 0 %}
  31. {% set nextOffset = 8 %}
  32. {% elseif currentIndex == 1 %}
  33. {% set nextOffset = 7 %}
  34. {% elseif currentIndex == 2 %}
  35. {% set nextOffset = 6 %}
  36. {% elseif currentIndex == 3 %}
  37. {% set nextOffset = 5 %}
  38. {% else %}
  39. {% set nextOffset = 4 %}
  40. {% endif %}
  41.  
  42. <ul>
  43.  
  44. {# Get all previous entries #}
  45. {% if currentIndex != -1 %}
  46. {% set prevIds = everyId %}
  47.  
  48. {% for item in craft.entries.id(prevIds).orderBy(paginationOrder).offset(currentIndex - prevOffset).all() if item.postDate|date('U') > entry.postDate|date('U') %}
  49. <li>
  50. <a href="{{item.url}}">{{item.title}}</a>
  51. </li>
  52. {% endfor %}
  53. {% endif %}
  54.  
  55. {# Current entry #}
  56. <li>
  57. <strong>{{entry.title}}</strong>
  58. </li>
  59.  
  60. {# Get all next entries #}
  61. {% if currentIndex != -1 %}
  62.  
  63. {# Get next ID's and offset #}
  64. {% set nextIds = everyId | slice(currentIndex+1, nextOffset) %}
  65. {% set nextEntries = craft.entries.id(nextIds).fixedOrder(true) %}
  66.  
  67. {% for nextEntry in nextEntries.all() %}
  68. <li>
  69. <a href="{{nextEntry.url}}">{{ nextEntry.title }}</a>
  70. </li>
  71. {% endfor %}
  72. {% endif %}
  73. </ul>
Add Comment
Please, Sign In to add comment