What I’m trying to accomplish is when a user is on an individual blog article/post, I want to display unique “related articles” based off of matching tags.
Here’s what I have so far:
{% for tag in article.tags %}
{% assign counter = 0 %}
{% for article in blog.articles %}
{% if article.tags contains tag and counter < 2 %}
{% assign counter = counter | plus: 1 %}
<li class="well">
{% if article.excerpt.size > 0 %}
<div class="thumb-article">
<a href="{{ article.url }}">
{{ article.excerpt }}
</a>
</div>
{% endif %}
<h3><a href="{{ article.url }}">{{ article.title }}</a></h3>
<p>{{ article.content | strip_html | strip_newlines | truncatewords: 40 }}</p>
</li>
{% endif %}
{% endfor %}
{% endfor %}
Surprisingly, (to me since this is my first experience with Shopify and liquid) it works, just a little too well as it gets duplicate posts.
Is there some way I can keep it from getting duplicate articles?
2
Answers
This thread has what you need: Shopify liquid get related blog posts
It creates an empty related posts variable then defines it in a loop that looks through other posts of that same category. To repeat its answer:
Go to that link above to see the full response.
For those who also searching how to implement related blog posts to a blog not product like in the links provided, there is the fix for code above:
split by anything you wouldn’t find in handle to create array
ending up with something like this: