Collecting my attempts to improve at tech, art, and life

Added Suggested Posts

Tags: jekyll site tools

I started writing about PNWPHP but got distracted and added “Suggested Reading” to my posts instead.

This weekend I attended PNWPHP. It was great. Instead of writing about that, I started looking up how to manage Related Posts in Jekyll. There are built-in options to do that, but they never satisfied me. I found Wenli Zhang’s post “Jekyll Related Posts without Plugin” and understood a little better.

Okay, no. Not really. But that post led me to related_posts-jekyll-plugin and this fork by jumanji27 which is friendlier for current Jekyll releases.

Since it’s a little more fiddly than a regular plugin, I hand-copied the code into a file _plugins/related-posts.rb and added a nice little “Suggested Posts” footer to the post template.

<article itemscope itemtype="http://schema.org/BlogPosting">
  ...
  <footer>
    <h3>Suggested Posts</h3>
    <p>
      {% for post in site.related_posts limit:5 %}
      <a href="{{ post.url }}" class="post-link">{{ post.title }}</a>{% unless forloop.last %}, {% endunless %}
      {% endfor %}
    </p>
  </footer>
</article>

I limited it to five related posts. Some posts will have a hundred related posts due to the way I imported content from my other sites.

Incidentally, I think this plugin does a better job picking related posts than Jekyll’s built-in approach, though I have no idea why. Jekyll usually ended up with nothing related, and just defaulted back to the most recent posts. This works better for me.

For the moment I prefer a comma-delimited dump of posts. Later I may play with some list styling. However, I did have the problem of post titles being broken up by line breaks. A little white-space: nowrap in the right spot fixes that.

a.post-link {
  white-space: nowrap;
}

Now the text of each post link stays together.

Just wanted to share all this. Have fun!


Added to vault 2024-01-15. Updated on 2024-01-26