Show specific blog articles on collection pages

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 }}’ %}

{{ article.title }}

{% endif %}

{% endfor %}

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

{% if article.tags contains prod_tag %}

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

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

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

:^)

Hello,

There is a new Shopify app that helps you insert blog Articles in Collections. Check it out here: https://apps.shopify.com/blog-articles-in-collection

For each of your collections, you can add a product slot to promote your best blog articles!

In this new product slot, you can display:

  • Blog title or custom text
  • Article title
  • Article main image

You can customize the way the text is displayed (color, size, background-color).

I am the creator of this app, feel free to ask more info :slightly_smiling_face:

Here is the code that I used on the Dawn theme. It works fine.
So I’m pulling the collection handle and splitting it into multiple words and if the article title matches any of those words it will put that article on the collection page. Here is the code:

{% assign collection_handle = collection.handle | replace: ‘-’, ’ ’ | split: ’ ’ %}
{% assign article_count = 0 %}

{% for article in blogs[‘your-blog-handle’].articles %}
{% if article.handle contains collection_handle.first or article.handle contains collection_handle[1] %}
{% if article_count < 3 %}

{{ article.image.src.alt | escape }}

{{ article.title }}

{{ article.published_at | time_tag: format: ‘date’ }}

{{ article.author }}

{%- if article.excerpt.size > 0 -%} {{ article.excerpt | strip_html | truncatewords: 30 }} {%- else -%} {{ article.content | strip_html | truncatewords: 30 }} {%- endif -%}

{% assign article_count = article_count | plus: 1 %} {% else %} {% break %}
{% endif %}

{% endif %}

{% endfor %}