Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@stravid
Created November 15, 2012 14:22
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save stravid/4078840 to your computer and use it in GitHub Desktop.
Save stravid/4078840 to your computer and use it in GitHub Desktop.
Category aware next attribute for a Jekyll post
module Jekyll
class CategoryAwareNextGenerator < Generator
safe true
priority :high
def generate(site)
site.categories.each_pair do |category_name, posts|
posts.sort! { |a, b| b <=> a }
posts.each do |post|
position = posts.index post
if position && position < posts.length - 1
category_next = posts[position + 1]
else
category_next = nil
end
post.data["#{category_name}_next"] = category_next unless category_next.nil?
end
end
end
end
end
@gerardbm
Copy link

gerardbm commented Apr 15, 2020

Hi! Is it possible to adapt this code to follow the order of the posts (configured in the front-matter)?

I use this in the loop:

{%- assign sorted_posts = site.categories['category'] | sort: 'order' -%}

And this in the front-matter: sort: 57

@stravid
Copy link
Author

stravid commented Apr 15, 2020

I guess it is, you have to ask someone who is familiar with Jekyll. I haven't worked with it for almost 8 years, so I'm of no help.

@gerardbm
Copy link

Oh, ok, thank you! Later I will try to implement it.

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