{% set relatedPosts = [] %} {% if tags %} {% for post in collections.posts %} {% if post.url != page.url %} {% set commonTags = 0 %} {% for tag in tags %} {% if post.data.tags and post.data.tags.includes(tag) %} {% set commonTags = commonTags + 1 %} {% endif %} {% endfor %} {% if commonTags > 0 %} {% set relatedPosts = (relatedPosts.push({post: post, score: commonTags}), relatedPosts) %} {% endif %} {% endif %} {% endfor %} {% endif %} {% if relatedPosts.length > 0 %} {% set sortedPosts = relatedPosts | sort(false, false, 'score') | reverse %}

Related Posts

{% for item in sortedPosts | slice(0, 3) %} {% set post = item.post %}

{{ post.data.title }}

{{ post.data.description or (post.templateContent | striptags | truncate(100)) }}

{% if post.data.tags %}
{% for tag in post.data.tags | slice(0, 2) %} #{{ tag }} {% endfor %}
{% endif %}
{% endfor %}
{% endif %}