How To Add Related Posts In Jekyll To Increase Engagement

How To Add Related Posts In Jekyll To Increase Engagement

What Are Related Posts?

Related Posts Setup — Jekyll Environment

Related Posts Code

<!-- Adds related posts to the end of an article -->

<h2>Enjoy Reading This Article?</h2>

<p>Here are some more articles you might like to read next:</p>


{% assign maxRelated = 5 %}
{% assign minCommonTags = 1 %}
{% assign maxRelatedCounter = 0 %}


<ul>
{% for post in site.posts %}

{% assign sameTagCount = 0 %}
{% for tag in post.tags %}
{% if post.url != page.url %}
{% if page.tags contains tag %}
{% assign sameTagCount = sameTagCount | plus: 1 %}
{% endif %}
{% endif %}
{% endfor %}


{% if sameTagCount >= minCommonTags %}

<li>

<a href="{{ site.baseurl }}{{ post.url }}">{{ post.title }}</a>

</li>

{% assign maxRelatedCounter = maxRelatedCounter | plus: 1 %}

{% if maxRelatedCounter >= maxRelated %}

{% break %}
{% endif %}
{% endif %}
{% endfor %}

</ul>
<!-- Adds related posts to the end of an article -->

<h2 class="text-3xl font-semibold mb-4 mt-12" >Enjoy Reading This Article?</h2>

<p class="mb-2">Here are some more articles you might like to read next:</p>


{% assign maxRelated = 5 %}
{% assign minCommonTags = 1 %}
{% assign maxRelatedCounter = 0 %}


<ul class="list-disc pl-8">
{% for post in site.posts %}

{% assign sameTagCount = 0 %}
{% for tag in post.tags %}
{% if post.url != page.url %}
{% if page.tags contains tag %}
{% assign sameTagCount = sameTagCount | plus: 1 %}
{% endif %}
{% endif %}
{% endfor %}


{% if sameTagCount >= minCommonTags %}

<li class="my-2">

<a class="text-pink-700 underline font-semibold hover:text-pink-800" href="{{ site.baseurl }}{{ post.url }}">{{ post.title }}</a>

</li>

{% assign maxRelatedCounter = maxRelatedCounter | plus: 1 %}

{% if maxRelatedCounter >= maxRelated %}

{% break %}
{% endif %}
{% endif %}
{% endfor %}

</ul>
 {% assign maxRelated = 5 %}
{% assign minCommonTags = 1 %}
{% assign maxRelatedCounter = 0 %}
  {% for post in site.posts %}
{% endfor %}
 {% assign sameTagCount = 0 %}
 {% for tag in post.tags %}

{% if post.url != page.url %}

{% if page.tags contains tag %}

{% assign sameTagCount = sameTagCount | plus: 1 %}

{% endif %}

{% endif %}

{% endfor %}
{% if sameTagCount >= minCommonTags %}

<li>
<a href="{{ site.baseurl }}{{ post.url }}">{{ post.title }}</a>
</li>

{% assign maxRelatedCounter = maxRelatedCounter | plus: 1 %}

{% if maxRelatedCounter >= maxRelated %}
{% break %}
{% endif %}

{% endif %}

Conclusion

I talk about Design + Productivity. I also share my journey as a creator. Everyone can design! My new eBook: https://gum.co/design-faster-design-better