Link to related posts

Overview

In the layout for the post we can use the variable site.related_posts:

...
<ul class="related_posts">
  {% for post in site.related_posts %}
    <li><a href="{{ post.url }}">{{ post.title }}</a></li>
  {% endfor %}
</ul>
...

By default this will output the ten most recent posts. If you run jekyll with the --lsi flag, it scans the content and tries to actually find relevant posts.

Limit

To display a maximum of three posts we can add a limit:

...
<ul class="related_posts">
  {% for post in site.related_posts limit: 3 %}
    <li><a href="{{ post.url }}">{{ post.title }}</a></li>
  {% endfor %}
</ul>
...