Skip to content

Instantly share code, notes, and snippets.

@nistude
Created August 14, 2011 09:05
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save nistude/1144723 to your computer and use it in GitHub Desktop.
Save nistude/1144723 to your computer and use it in GitHub Desktop.
A category list tag for jekyll
# place this file in your plugins directory and add the tag to your sidebar
#$ cat source/_includes/custom/asides/categories.html
#<section>
# <h1>Categories</h1>
# <ul id="categories">
# {% category_list %}
# </ul>
#</section>
module Jekyll
class CategoryListTag < Liquid::Tag
def render(context)
html = ""
categories = context.registers[:site].categories.keys
categories.sort.each do |category|
posts_in_category = context.registers[:site].categories[category].size
html << "<li class='category'><a href='/categories/#{category}/'>#{category} (#{posts_in_category})</a></li>\n"
end
html
end
end
end
Liquid::Template.register_tag('category_list', Jekyll::CategoryListTag)
@davidxia
Copy link

I had trouble with ampersands in category names, eg 'Tech & Prod'. The above generated a url that was missing the '-and-' that Jekyll creates. I found this worked

category_url = File.join(category_dir, category.to_url)

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