Created
August 14, 2011 09:05
-
-
Save nistude/1144723 to your computer and use it in GitHub Desktop.
A category list tag for jekyll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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