Skip to content

Instantly share code, notes, and snippets.

@travisjupp
Created August 21, 2017 22:38
Show Gist options
  • Save travisjupp/fba6cfde4a0abf013efc6149d809ce3d to your computer and use it in GitHub Desktop.
Save travisjupp/fba6cfde4a0abf013efc6149d809ce3d to your computer and use it in GitHub Desktop.
{% assign ordered_posts = page.parent.children | sort:'published_at' %}
{% for post in ordered_posts %}
{% if post.id == page.id %}
{% assign previous_post_number = forloop.index | minus:1 %}
{% assign next_post_number = forloop.index | plus:1 %}
{% endif %}
{% endfor %}
{% for post in ordered_posts %}
{% if forloop.index == previous_post_number %}
{% assign previous_post = post %}
{% elsif forloop.index == next_post_number %}
{% assign next_post = post %}
{% endif %}
{% endfor %}
@travisjupp
Copy link
Author

Put the code above at the very top of your two blog post templates (on the first line, above all the other code). Those two templates are:
“pages_show_blog_post.html” for blog posts with the sidebar activated
“pages_show_blog_post_wide.html” for posts without the sidebar
You can then access any page variable for the previous post and the next post — see the full list of available variables here: http://nationbuilder.com/page_variables.
An example implementation is to add a pair of buttons on your page somewhere, which could each link to the previous and next posts (and optionally include the headline of each):

@travisjupp
Copy link
Author

You want to wrap your buttons in those liquid {% if %} conditionals so you don’t get a ‘previous’ link on your oldest post, or a ‘next’ link on your newest post.

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