How to Add Category List to Your Octopress Blog

- - posted in TechChangeWorld - tagged by blogger, octopress | Comments

1,generate category list

save code to the file plugins/category_list_tag.rb

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
        category_dir = context.registers[:site].config['category_dir']
        category_url = File.join(category_dir, category.gsub(/_|\P{Word}/, '-').gsub(/-{2,}/, '-').downcase)
        html << "<li class='category'><a href='/#{category_url}/'>#{category} (#{posts_in_category})</a></li>\n"
      end
      html
    end
  end
end

Liquid::Template.register_tag('category_list', Jekyll::CategoryListTag)

This plugin will register a tag『in xml 』 called catetory_list in liquid.
All your category in you blog, will be organised in li.

2,add aside

copy those code to source/_includes/asides/category_list.html

del the ‘\’

1
2
3
4
5
6
<section>
  <h1>Categories</h1>
  <ul id="categories">
    {\% category_list %}
  </ul>
</section>

then modify config file _config.yml:

1
default_asides: [asides/category_list.html, asides/recent_posts.html]

or include the category_list.html in some file

Comments