Search code examples
jekylljekyll-extensions

How can I sort posts by tags in Jekyll?


I'm not familiar with Ruby and I know there's a common Jekyll plugin for doing this but I tried all day and have not been able to get it working.

I added this plugin:

module Jekyll
  class TagIndex < Page
    def initialize(site, base, dir, tag)
      @site = site
      @base = base
      @dir = dir
      @name = 'index.html'
      self.process(@name)
      self.read_yaml(File.join(base, '_layouts'), 'tag_index.html')
      self.data['tag'] = tag
      tag_title_prefix = site.config['tag_title_prefix'] || 'Posts Tagged &ldquo;'
      tag_title_suffix = site.config['tag_title_suffix'] || '&rdquo;'
      self.data['title'] = "#{tag_title_prefix}#{tag}#{tag_title_suffix}"
    end
  end
  class TagGenerator < Generator
    safe true
    def generate(site)
      if site.layouts.key? 'tag_index'
        dir = site.config['tag_dir'] || 'tag/tag'
        site.tags.keys.each do |tag|
          write_tag_index(site, File.join(dir, tag), tag)
        end
      end
    end
    def write_tag_index(site, dir, tag)
      index = TagIndex.new(site, site.source, dir, tag)
      index.render(site.layouts, site.site_payload)
      index.write(site.dest)
      site.pages << index
    end
  end
end

and to _config.yml I added

tag_dir: /tag

I display the list of tags with this include file:

<div class="tags modal">
    <ul>
        {% for tag in site.tags %}
        <li>
            <a href="/tag/{{ tag | first | slugize }}/">
                {{ tag | first }}
            </a>
        </li>
        {% endfor %}
    </ul>
</div>

And when I try to select a tag, the url changes appropriately but nothing else changes. It does not display my tag_index template or filter the tags.

The site can be previewed and full source is available here. I've been banging my head against the wall for a while and can't for the damned of me figure out what I'm not doing right.


Solution

  • Only some plugins are supported by github pages. Your tag pages are not currently generated.

    If you want to use your tag plugin, you have to

    • generate you site locally
    • add an empty .nojekyll file
    • push the generated files in you master branch
    • you can also push you code in an other branch like code