Displaying Tagged Articles Based on Product Name

{% for article in blogs.variety-guides.articles %}

{% assign TVname = product.title  | downcase | split: "-" | slice: 0 | string %}
{{ TVname }}
		       {% if article.tags contains "{{ TVname }}" %}
		<img src="{{ article.image | img_url: '200x200' }}" alt=""><br>
		  	 	{{- article.title | link_to: article.url }}
		       {% else %}
		        <p>No Articles for this Product.</p>
		       {% endif %}
{% endfor %}

Hello all!

I have a feeling I am going about this the completely wrong way, but essentially at the bottom of each individual product page I want to pull up relevant articles tagged with the product name in a quick and dirty way, in this example the product title would be something like “Samsung TU700D - 65” 4k TV" and the TVname string would look for all articles written and tagged with “samsungtu700d” and display them at the bottom of the page. I am currently having trouble comparing the TVname string to the article.tag, I’m a bit lost here.

Any help is greatly appreciated.

Hi @Liquid_Noob

I believe you can just use the variable directly rather than using curly braces or quotes?

i.e.

{% if article.tags contains TVname %}

Let me know if that helps?

Unfortunately that is not the solution, but when I put a specific article tag in the if statement my articles pop up so the code is seemingly working, just not when I use the string.

Thanks for giving it a shot!

Hi @Liquid_Noob

When you render it to the screen - {{ TVname }} - are you seeing exactly the phrase you need?

I managed to get the result you’re trying to get by using this code:

{% assign TVname = product.title  | downcase | split: "-" | first | replace: " ", "" %}

The removal of the spaces is I think what should get you the exact match you’re looking for with the tag format.

Let me know if that helps?

1 Like

Thank you 2B! This seems to do the trick.