Published on

Displaying Related Posts in Octopress

Authors

I configured my Octopress blog to show related posts under articles. Octopress has built-in support for related posts, so I used that feature.

Edit the Octopress templates

Create source/_includes/post/related_posts.html and add the content below. The limit value controls how many related posts are displayed, so adjust it as needed.

{% raw %} {% if site.related_posts %}
<h2>Related posts</h2>
<ul class="posts">
  {% for post in site.related_posts limit:5 %}
  <li class="related">
    <a href="{{ root_url }}{{ post.url }}">{{ post.title }}</a>
  </li>
  {% endfor %}
</ul>
{% endif %} {% endraw %}

Then include that HTML from the post template. Open source/_layouts/post.html and add the following line. Adjust the location to wherever you want it to appear. This time I placed it after the sharing buttons.

{% raw %}
<footer>
  <p class="meta">
    {% include post/author.html %}
    {% include post/date.html %}{% if updated %}{{ updated }}{% else %}{{ time }}{% endif %}
    {% include post/categories.html %}
  </p>
  {% unless page.sharing == false %}
    {% include post/sharing.html %}
  {% endunless %}
+ {% include post/related_posts.html %}
  <p class="meta">
    {% if page.previous.url %}
{% endraw %}

Add the following flag to _config.yml.

lsi: true

After that, rake generate will generate related posts. This is enough to make it work, but the processing is extremely slow. It seems to spend a long time classifying posts at the Running the classifier step. So, as the message suggests, install gsl to improve performance.

$ rake generate
## Generating Site with Jekyll
identical source/stylesheets/screen.css
Configuration from /home/daisuke/Projects/blog/octopress/_config.yml
Notice: for 10x faster LSI support, please install http://rb-gsl.rubyforge.org/
Building site: source -> public
Running the classifier... this could take a while.
........................................

Install GSL

Run the commands below to install gsl. If you install it through apt, version 1.16 is installed, but the newer version apparently does not work, so install 1.14 instead.

wgethttp://ftp.gnu.org/gnu/gsl/gsl1.14.tar.gzwget http://ftp.gnu.org/gnu/gsl/gsl-1.14.tar.gz tar xvf gsl-1.14.tar.gz cdgsl1.14cd gsl-1.14 ./configure makemake sudo make install

Check that gsl 1.14 is installed with the following command.

$ gsl-config --version 1.14

Add the following entry to the development group in Gemfile, then run bundle install.

gem 'gsl'

After that, if you run rake generate, you should see that the classifier step is much faster. It still takes one to two minutes, though.

References