Show specific blog articles on collection pages

Shopify Expert
52 1 0

Hi.

I'm trying to show a list of Blog Articles on each Collection page that has the same Product Tags used to poluate each Collection. For example, I have a Collection created to show Products with the Product Tag 'Apple'. I then want to show a list of all the Blog Articles on that Apple Collection page that have the Article Tag 'Apple'. 

This is the code I have written. I'm trying to Capture the Product Tag and then get the code to use this Product Tag to find all the Blog Articles I have written with the same Tag.

I can get the Capture to capture the correct Product Tag {{ prod_tag }} but I cannot use the Capture in the next code I have to find the Blog Articles.

Can anyone help? I'm really stuck on this one. Thanks

This is all the code...

{% capture prod_tag %}
          {% for tag in current_tags %}
            {{ tag | remove: 'brands_' | replace: '-', ' ' }}
        {% endfor %}
      {% endcapture %}
      
      {{ prod_tag }} <--- this is just a test for me to see that I the capture code works
      
     {% for article in blogs.our-brands.articles %}
      
              {% if article.tags contains '{{ prod_tag }}' %}

                    <a href="{{ article.url }}">{{ article.title }}</a>

            {% endif %}

      {% endfor %}
      

0 Likes
Shopify Expert
685 0 68

This will do what you intend if prod_tag is assigned correctly:

{% if article.tags contains prod_tag %}
I'm a million different people
0 Likes
Shopify Expert
52 1 0

Hi Ricky

Thanks for your reply. I tried that just now but still not working.

Strange as the Capture code I've written does give the right results as shown in my screenshot. However, when I add the 'prod_tag' to the article code below this (green) it doesn't work at all.

Any ideas?

Nigel

0 Likes
Shopify Expert
685 0 68

In green your text says "shows correct tags"; plural. What is the output of prod_tag? If it is capturing multiple Tags then you need to capture and create an array, then step thru both arrays : a) loop thru the Article's Tags and then for each in turn, b) loop thru the captured Tags — to check for each Article Tag whether any of the capatured Tags matches.

e.g. if Article Tags contains "bananas, apples" and prod_tag contains "oranges, apples" you will need to loop so that the iterations are as follows:

  1. bananas to oranges = false
  2. bananas to apples = false
  3. apples to oranges = false
  4. apples to apples = true
I'm a million different people
0 Likes
Highlighted
Shopify Expert
52 1 0

Hi Ricky,

I've managed to get it working without using the Capture code. I was making it vastly more difficult than it needed to be.

Thanks for taking the time to reach out though. 

Nigel

:^)

0 Likes