Skip to content

Instantly share code, notes, and snippets.

@tvdsluijs
Created July 11, 2018 12:48
Show Gist options
  • Save tvdsluijs/49dd4523bb919e85c24fa32e3834b719 to your computer and use it in GitHub Desktop.
Save tvdsluijs/49dd4523bb919e85c24fa32e3834b719 to your computer and use it in GitHub Desktop.
Better pagination for Jekyll on Github pages, also see :
{% if paginator.total_pages > 1 %}
<div class="pagination">
{% if paginator.previous_page %}
<a class="ml-1 mr-2" href="{{ paginator.previous_page_path | prepend: site.baseurl | replace: '//', '/' }}">&laquo; {{site.str_previous_page}}</a>
{% else %}
<span>&laquo; {{site.str_previous_page}}</span>
{% endif %}
{% assign page_start = paginator.page | minus: site.pagination_nr %}
{% assign page_end = paginator.page | plus: site.pagination_nr %}
{% for page in (page_start..page_end) %}
{% if page < 1 %}
<!-- do freaking nothing -->
{% elsif page == paginator.page %}
<span class="ml-1 mr-1">{{ page }}</span>
{% elsif page == 1 %}
<a class="ml-1 mr-2" href="{{ paginator.previous_page_path | prepend: site.baseurl | replace: '//', '/' }}">{{ page }}</a>
{% else %}
<a class="ml-1 mr-2" href="{{ site.paginate_path | prepend: site.baseurl | replace: '//', '/' | replace: ':num', page }}">{{ page }}</a>
{% endif %}
{% endfor %}
{% if paginator.next_page %}
<a class="ml-1 mr-2" href="{{ paginator.next_page_path | prepend: site.baseurl | replace: '//', '/' }}">{{site.str_next_page}} &raquo;</a>
{% else %}
<span>{{site.str_next_page}} &raquo;</span>
{% endif %}
</div>
{% endif %}
@MihajloNesic
Copy link

Thank you for this!

I suggest adding these 2 lines of code after the {% if page < 1 %}, because you will get a higher number of pages than you actually have.

{% elsif page > paginator.total_pages %}
      <!-- Do nothing -->

So the whole for loop would be:

{% for page in (page_start..page_end) %}
    {% if page < 1 %}
      <!-- do freaking nothing -->
    {% elsif page > paginator.total_pages %}
      <!-- Do nothing -->
    {% elsif page == paginator.page %}
      <span class="ml-1 mr-1">{{ page }}</span>
    {% elsif page == 1 %}
      <a  class="ml-1 mr-2" href="{{ paginator.previous_page_path | prepend: site.baseurl | replace: '//', '/' }}">{{ page }}</a>
    {% else %}
      <a  class="ml-1 mr-2" href="{{ site.paginate_path | prepend: site.baseurl | replace: '//', '/' | replace: ':num', page }}">{{ page }}</a>
    {% endif %}
{% endfor %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment