Generating monthly archives with Jekyll

Continuing my series of posts about Jekyll I will show in this post how to generate monthly archives. This is the index you see when you enter this url:

The difference between Jekyll and any other blog system is that Jekyll generate the site at build time, while the others generate the page at runtime with data coming from a database.

The first thing you will need is a jekyll plugin in the "_plugins" folder as follow:

module Jekyll
  class ArchiveIndex < Page
    def initialize(site, base, dir, period, posts)
      @site = site
      @base = base
      @dir = dir
      @name = 'index.html'
      self.read_yaml(File.join(base, '_layouts'), 'archive_index.html')['period'] = period['period_posts'] = posts
      archive_title_prefix = site.config['archive_title_prefix'] || 'Archive: “'
      archive_title_suffix = site.config['archive_title_suffix'] || '”'['title'] = "#{archive_title_prefix}#{period["month"]}-#{period["year"]}#{archive_title_suffix}"
  class ArchiveGenerator < Generator
    safe true
    def generate(site)
      if site.layouts.key? 'archive_index'
        site.posts.group_by{ |c| {"month" =>, "year" =>} }.each do |period, posts|
          archive_dir = File.join(period["year"].to_s(), "%02d" % period["month"].to_s())
          write_archive_index(site, archive_dir, period, posts)
    def write_archive_index(site, dir, period, posts)
      index =, site.source, dir, period, posts)
      index.render(site.layouts, site.site_payload)
      site.pages << index

Shame on me!! please pardon my ruby skills.

This will generate an html file index.html in the folder /YYYY/MM.

And then in the "layout" folder I wrote a template for this archive_index.html (you can set another name in _config.yml):

layout: default
meta-robots: "noodp, noydir, noindex, noarchive, follow"

<h1> Posts archive: {{ page.period["month"] }} - {{page.period["year"]}} </h1><br>
{% for post in page.period_posts %}
    <a href="">{{ post.title }}</a><br>
    <span class="entry-meta">Published: {{ | date: "%B %d, %Y" }}</span>
{% endfor %}

blog comments powered by Disqus
  • Categories

  • Archives