Skip to content

Instantly share code, notes, and snippets.

@opattison
Last active December 20, 2015 21:39
Show Gist options
  • Save opattison/6199158 to your computer and use it in GitHub Desktop.
Save opattison/6199158 to your computer and use it in GitHub Desktop.
A pattern for category-limited navigation using Liquid in Jekyll. The `if` statement only generates a nav link if the URL for the next/previous post exists *and* if the category for the next post is of the same type. This pattern assumes that one is using categories to describe types of post (e.g. writing, photos, videos and so on) to build mult…
<footer class="post-nav">
{% if page.next.url and page.next.categories contains 'writing' %}
<a class="more next" href="{{page.next.url}}" title="Later article: {{page.next.title}}">Next</a>
{% endif %}
{% if page.previous.url and page.previous.categories contains 'writing' %}
<a class="more previous" href="{{page.previous.url}}" title="Earlier article: {{page.previous.title}}">Previous</a>
{% endif %}
</footer>
@opattison
Copy link
Author

@Sakrecoer
Copy link

Thank you for the pattern, but for some reason, I am having trouble getting it to act consistently. Each and every post, the sequence will break and the "previous" post is rendered either without the "next" link or the "previous". I cannot get my head around the reason behind this...? All post are categorized correctly and using the same _layout file... Could you point to some brighter direction? 🍪 :)

I hope this is the right place to ask about the pattern, and thank you for your time!

@Sakrecoer
Copy link

i think i found the reason for this to be unconsistent in my scenario. The loop stops if the next/previous post exists but is not in the specified category.... working on it :) i'll get back whenever i figure this out....

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