Skip to content

Instantly share code, notes, and snippets.

@budparr
Last active September 29, 2021 10:55
Show Gist options
  • Star 31 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save budparr/3e637e575471401d01ec to your computer and use it in GitHub Desktop.
Save budparr/3e637e575471401d01ec to your computer and use it in GitHub Desktop.
Previous Next Links for Jekyll Collections
{% 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 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 %}
<script>
document.body.onkeyup = function(e){
if (e.keyCode == '37') { window.location = '{{prevurl}}'; }
if (e.keyCode == '39') { window.location = '{{nexturl}}'; }
};
</script>
{% if prevurl %}<a href="{{prevurl}}" class="prev">PREV {{prevurl}}</a>{% endif %}<br />
{% if nexturl %}<a href="{{nexturl}}" class="nxt">Next {{nexturl}}</a>{% endif %}
@citelao
Copy link

citelao commented Oct 19, 2019

This still works in 2019! However, Jekyll collections (that generate pages with output: true at least) seem to have the built-in page.previous and page.next variables, too, so you may not need this.

See any guide on using those variables for posts. It should apply.

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