Web Notes
2016.08.20
Using Liquid in Jekyll - Live with Demos
Liquid is a simple template language that Jekyll uses to process pages for your site. With Liquid you can output complex contents without additional plugins.
Jekyll features a simple “related posts” variable per post page with site.related_posts
, which just contains 10 most recent posts by default. It only works perfectly when lsi
(latent semantic indexing) option was enabled.1 As explained in Jekyll’s documents, with lsi
features enabled, the build process will slow down. What’s more, it’s not supported by GitHub Pages.
Now, you can use the
gsl
library to boost the Jekyll building process while enablelsi
option, see Enable Jekyll’s LSI for related posts with fast build speed.The method below still works as well… 🥳
So I find a post that solved this issue by using only Liquid tags to generate the related posts with tags
or categories
. It works by going through the related posts collection and selecting the posts that contain any tags (or categories) in common with the current post, up to a self-defined limit number. If there are enough posts to fill that limit, fine, it stops there. Otherwise, it goes again through the most recent, possibly unrelated posts, and outputs them until the limit is finally reached.
The full snippet is presented below.2 Simply adjust the HTML structure fitting your needs.
{% assign RELATED_POSTS_THRESHOLD = 3 %}
<ul>
{% assign related_post_count = 0 %}
{% for post in site.related_posts %}
{% if related_post_count == RELATED_POSTS_THRESHOLD %}
{% break %}
{% endif %}
{% for tag in post.tags %}
{% if page.tags contains tag %}
<li>
<h3>
<a href="{{ site.url }}{{ post.url }}">
{{ post.title }}
<small>{{ post.date | date_to_string }}</small>
</a>
</h3>
</li>
{% assign related_post_count = related_post_count | plus: 1 %}
{% break %}
{% endif %}
{% endfor %}
{% endfor %}
{% assign posts_left = RELATED_POSTS_THRESHOLD | minus: related_post_count %}
{% unless posts_left == 0 %}
{% for post in site.related_posts %}
{% if posts_left == 0 %}
{% break %}
{% endif %}
{% assign already_related = false %}
{% for tag in post.tags %}
{% if page.tags contains tag %}
{% assign already_related = true %}
{% break %}
{% endif %}
{% endfor %}
{% unless already_related %}
{% assign posts_left = posts_left | minus: 1 %}
<li>
<h3>
<a href="{{ site.url }}{{ post.url }}">
{{ post.title }}
<small>{{ post.date | date_to_string }}</small>
</a>
</h3>
</li>
{% endunless %}
{% endfor %}
{% endunless %}
</ul>
You can also replace the site.related_posts
with site.posts
if you want more posts beyond the ten recent posts.
Frank Lin
Web Notes
2016.08.20
Liquid is a simple template language that Jekyll uses to process pages for your site. With Liquid you can output complex contents without additional plugins.
Web Notes
2019.11.21
Install right tools to speed up Jekyll's latent semantic indexing (lsi) for real related posts.
Web Notes
2021.05.22
Just updated my site search from Simple-Jekyll-Search to Algolia, with the help of Netlify build plugin. In this post, I'll talk about both of them, just choose the one in your manner.