Skip to content

Instantly share code, notes, and snippets.

@atikju
Created February 25, 2021 11:24
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save atikju/90a3aa34666ed5f51998a576583132ba to your computer and use it in GitHub Desktop.
Save atikju/90a3aa34666ed5f51998a576583132ba to your computer and use it in GitHub Desktop.
Show related articles based on article tags - Shopify
@henryvu-gempages
Copy link

thank you so much! 👍 💯

@joshspires
Copy link

Is it possible to prevent posts showing up multiple times?

@pixelolabs
Copy link

I need to show only 3 article , how I can show them?

@joshspires
Copy link

I need to show only 3 article , how I can show them?

You can limit the number of posts shown by adding the limit to the for loop on line 7.

{% for related_article in blog.articles limit:3 %}

@nekosan01
Copy link

Thank you for the code, overall works great but as @joshspires mentioned. There is a slight mistake in the code which sets back the skip_articles to the article.handle in every single iteration. This means that the same article can be rendered multiple times. The solution for this one is moving the {% assign skip_articles = article.handle | split: '.....' %} part above the {% for tag in article.tags %} .

This means that the skip_articles won't reset to the article.handle in every iteration.

@joshspires
Copy link

@nekosan01 thanks for that! I've updated my fork

@Jade6M
Copy link

Jade6M commented Feb 3, 2023

Does anyone know how to get the next article when one has been skipped? I have a limit of 3 and it only shows two if the article handle is the same

@chunkysteveo
Copy link

chunkysteveo commented Jun 7, 2023

You are probably hitting the default limit of the blog article pagination - i.e. it's not searching all your blog articles. Add {% paginate blog.articles by 999 %} before the {% for related_article in blog.articles %} loop, and add {% endpaginate %} after the loop closes.

(Shopify doesn't like querying A LOT of articles, so I never told you this!)

@barafunda
Copy link

Hi,
I'm trying to get this code to work for articles with multiple tags. At the moment it works if the article has only one tag, as far as I was able to understand. Any ideas? I've very new to Shopify and liquid development.
Thanks in advance!

@chunkysteveo
Copy link

Hi, I'm trying to get this code to work for articles with multiple tags. At the moment it works if the article has only one tag, as far as I was able to understand. Any ideas? I've very new to Shopify and liquid development. Thanks in advance!

Are you after the query for articles containing ALL tags, e.g. tag1 and tag2 and tag3 etc, or articles that may contain tag1, or tag2, or tag3 etc?

@barafunda
Copy link

Thanks for the reply! Good point, didn't clarify properly.
It's articles that contain one or more of the multiple tags in the current article.

@chunkysteveo
Copy link

The original code will loop through all the tags of the article and pull out related articles with contain those tags - tag by tag, most likely in alphabetical order (not checked). The loop checking is limited by the counter variable. I suspect if you increase that value, it will have more time to loop through all tags before hitting the limit and stop checking.

The code has no 'weight' to article tags or preference other than (I assume) looping in alphabetical order through all article tags. If you think it's also missing articles, it may be hitting the pagination limit before finding them (if you have a lot of blog articles?).

@barafunda
Copy link

barafunda commented Jan 16, 2024

Apologies, the code is in fact working with multiple tags. The inconsistent results I'm getting are most likely related to errors in my own theme, rather than with this code. Thanks again for your help!

I think I understand your suggestion, tried and it didn't work. When an article has multiple tags it simply isn't displaying anything. It might be because the tags aren't 'split', and it's outputting the full string (ex: tag1tag2tag3).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment