Intro

You may have noticed this blog is running on Jekyll, as it uses the default theme. I wanted to spice things up a little bit and add a “Related Posts” section to the bottom of each post. That way, you fine readers can easily see other content on my blog that might also interest you. Also, I can maybe get a few more of those precious page views.

Jekyll has pretty great built-in support for doing this, but the documentation is lacking. I found a few blogs about it, but nothing got me over the finish line. I figured I’d go totally meta and write a blog about how I did this on this very blog.

Install Dependencies

OK, first, crack open your site’s Gemfile and add the classifier-reborn gem. This does some semantic analysis of your posts to figure out what’s more related to what.

group :jekyll_plugins do
  gem "jekyll-sitemap"
  gem "jekyll-seo-tag"
  gem "classifier-reborn"
end

Make sure to run bundle install to install it.

Configure Jekyll

We need to configure Jekyll to use the plugin, and we need to enable the LSI:

Now, open up _config.yml, enable “latent semantic indexing”:

lsi: true

You also need to add classifier-reborn to the plugins list:

plugins:
  - jekyll-sitemap
  - jekyll-seo-tag
  - classifier-reborn

Edit the Posts Layout

Ok, so Jekyll provides an object site with variables. One of those variables is related_posts. It will contain the most recent 10 posts unless you have LSI enabled. With LSI, you’ll get 10 posts listed in order of most related to least related. We’ll want to iterate through this list and print out some links on our page somewhere. The easiest way to do this is to edit the Post layout at /_layouts/post.html and add in the following section:

  <h2> Related Posts </h2>
  <div class="related">
    {% for post in site.related_posts limit:5 offset:1 %}
    <li>
      <a href="{{ post.url }}">{{ post.title }}</a>
    </li>
    {% endfor %}
  </div>

Add in the “Related Posts” heading, and the div just below it, or somewhere else if you want. The code will loop through the site.related_posts and print out some simple links with the post.title variable as the text.

You might have noticed the limit and offset directives. I thought 10 articles made too long of a list, so this cuts it down to just the first 5. Setting offset to 1 skips the first entry in the list, which will seemingly always be the article you’re already on…

Build the Site

We’re just about done. When you rebuild the site, Jekyll will have to do all the LSI stuff. If you have a lot of large posts it may take some time. You should see LSI output during the build process if you did everything correctly:

Generating...
Populating LSI...
Rebuilding index...

That’s it! Go forth and get those clicks.