Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnfmorton/6c395361d259b23409ee6559fd909d5c to your computer and use it in GitHub Desktop.
Save johnfmorton/6c395361d259b23409ee6559fd909d5c to your computer and use it in GitHub Desktop.
Previous Next Links for Jekyll Collections - with wrap around to beginning link and last link when needed
{% capture the_collection %}{{page.collection}}{% endcapture %}
{% if page.collection %}
{% assign document = site[the_collection] %}
{% endif %}
<h1>TITLE: {{ page.title }}</h1>
{% for links in document %}
{% if forloop.first %}
{% assign first = links %}
{% endif %}
{% if forloop.last %}
{% assign last = links %}
{% endif %}
{% if links.title == page.title %}
{% unless forloop.first %}
{% assign prevurl = prev.url %}
{% endunless %}
{% unless forloop.last %}
{% assign next = document[forloop.index] %}
{% assign nexturl = next.url %}
{% endunless %}
{% endif %}
{% assign prev = links %}
{% endfor %}
{% assign firsturl = first.url %}
{% assign lasturl = last.url %}
{% if prevurl %}<a href="{{prevurl}}" class="prev">Prev</a>{% else %}<a href="{{lasturl}}" class="prev">Prev</a>{% endif %}<br />
{% if nexturl %}<a href="{{nexturl}}" class="nxt">Next</a>{% else %}<a href="{{firsturl}}" class="prev">Next</a>{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment