0
votes

I am trying to pull through related blog posts if the article.tags == the string given within the section settings.

I have tried a few different variations but to no effect. My code looks like this (don't worry about the content within the if loop, this is all fixed into shape by the CSS):

{% for article in blogs.news.articles limit:1 %}
    {% if article.tags == section.settings.brand-news-tag | strip_html %}
        {% assign image_src = article.image.src | img_url: 'large' %}
        <div class="brand-page-featured-news-blogs">
            <div class="brand-page-featured-news-article">
                <a href="{{ article.url }}" class="box-link"></a>
                <div class="brand-page-featured-news-article-image" style="background-image: url({{ image_src }})">
                    <div class="brand-page-featured-news-article-image-contain">
                        <div class="brand-page-featured-news-article-image-overlay">
                        </div>
                    </div>
                </div>
                <div class="brand-page-featured-news-article-contain">
                    <h6 class="brand-page-featured-news-article-title">{{ article.title }}</h6>
                    <div class="brand-page-featured-news-article-content">
                        <p class="brand-page-featured-news-article-published">{{ article.published_at | date: "%d %B 20%y" }}</p>
                        <p class="brand-page-featured-news-article-text">{{ article.content }}</p>
                        <div class="brand-page-featured-news-article-button">
                            <div class="brand-page-featured-news-article-button-text">
                                Read More
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    {% endif %}
{% endfor %}
{% schema %}
    {
        "name": "Featured News",
        "settings": [
            {
                "id": "brand-news-tag",
                "type": "text",
                "label": "Brand News Tag",
                "default": "brandnametag"
            }
        ]
    }
{% endschema %}
{% stylesheet %}
{% endstylesheet %}
{% javascript %}
{% endjavascript %}

This is the line in question:

{% if article.tags == section.settings.brand-news-tag | strip_html %}

I have tried to use a few other variations like without the '| strip_html'. I have tried to put it inside quotes like this {% if article.tags == "'" and section.settings.brand-news-tag and "'" %}. I have also tried to use 'contains' opposed to an '=='.

How can I use a variable within the if statement?


I have tried using -

{% if article.tags contains section.settings.brand-news-tag %}

I have also tried without any if statement narrowing down the blogs.news. this works as expected. Meaning it is something to do with this if statement not comparing to the blog tags. Although I have directly copied the blog tag in from the blog post to go into the variable within the section.

This also doesn't work -

{% for article in blogs.news.articles limit:1 %}
    {% if section.settings.brand-news-tag != '' %}
        {% assign blogfilter = section.settings.brand-news-tag | strip %}
    {% endif %}
    {% if article.tags contains blogfilter %}

This also doesn't work (Goodfellow is the tag copied) -

{% for article in blogs.news.articles limit:1 %}
    {% if article.tags contains 'Goodfellow' %}

Here are some images of the blog side of things:

enter image description here

enter image description here

enter image description here

1

1 Answers

1
votes

If you look at the article object in Shopify documentation then article.tags return an array. So an array cannot be compared with a string using == equals operator. You are looking for contains as explained in the logical and comparison operators documentation.

So your code will become

{% if article.tags contains section.settings.brand-news-tag %}

Moreover, you don't need strip_html as the field type is text. So Shopify will take care of it. You can use strip filter to remove any space and tabs from the start and end of string just to be extra sure.

String Filters

For your particular scenario, you don't need the limit: 1 inside for loop because you don't know that first object will contain the tag. So you need to iterate over all the objects and break out of loop if condition is satisfied. Sample code

{% for article in blogs.news.articles %}
    {% if article.tags contains 'Goodfellow' %}
        {{article.tags}}
        {% break %}
    {% endif %}
{% endfor %}