Re: Show text only if there are product tags

Solved

How to display text only for products with tags in Liquid?

earthtoplanet
Explorer
46 6 8

Hello, novice here.

 

For products which have product tags, I want to show the text: "Related keywords: [Tag1], [tag2], [tag3] ..."

 

I've managed to get the tags to show up on the product page. However, for products which do not have product tags it just says "Related keywords:"

 

In Liquid, how do I write an if statement that essentially says "Only show the following if there is a value in the Product Tag field, otherwise show nothing"? 

 

Below is what I have so far. Appreciate your help!

 

Page with product tags: https://earthtoplanet.com/collections/cartoons/products/abundance-of-caution 

Page with no product tags: https://earthtoplanet.com/collections/cartoons/products/pythagoras-shortcut 

(Theme: Dawn)

 

{% if product.tags %}
              
                <div class="product_tags">
                
                  <font size="-1.5">
                  Related Keywords:
                </font>
               {% for tag in product.tags %}
       
                 <a class="link" href="/collections/all/{{ tag | handle }}" rel="tag" style="text-decoration: none">
                   <font size="-1.5">
                     <span>{{ tag }},</span>
                   </font>
                   {% unless forloop.last %}
                   {% endunless %}
                </a>
               {% endfor %}
           
              </div>
             
              {% else %}
              {% endif %}

 

Accepted Solution (1)

Savior
Shopify Partner
537 108 161

This is an accepted solution.

@earthtoplanet 

Please replace your code with the below code

 

 

{% assign tag_found = product.tags %}
        {% if tag_found != blank %}

        <div class="product_tags">

          <font size="-1.5">
            Related Keywords:
          </font>
          {% for tag in product.tags %}

          <a class="link" href="/collections/all/{{ tag | handle }}" rel="tag" style="text-decoration: none">
            <font size="-1.5">
              <span>{{ tag }},</span>
            </font>
            {% unless forloop.last %}
            {% endunless %}
          </a>
          {% endfor %}

        </div>
        {% endif %}

 

 


If helpful then please Like and Accept Solution. Want to modify or custom changes on store Hire me. Feel free to contact me on mail | Shopify Design Changes | Custom Modifications In to Shopify Theme

banned

View solution in original post

Replies 5 (5)

Savior
Shopify Partner
537 108 161

This is an accepted solution.

@earthtoplanet 

Please replace your code with the below code

 

 

{% assign tag_found = product.tags %}
        {% if tag_found != blank %}

        <div class="product_tags">

          <font size="-1.5">
            Related Keywords:
          </font>
          {% for tag in product.tags %}

          <a class="link" href="/collections/all/{{ tag | handle }}" rel="tag" style="text-decoration: none">
            <font size="-1.5">
              <span>{{ tag }},</span>
            </font>
            {% unless forloop.last %}
            {% endunless %}
          </a>
          {% endfor %}

        </div>
        {% endif %}

 

 


If helpful then please Like and Accept Solution. Want to modify or custom changes on store Hire me. Feel free to contact me on mail | Shopify Design Changes | Custom Modifications In to Shopify Theme

banned
earthtoplanet
Explorer
46 6 8

Ah hah!  !=blank was the term I was looking for. Thank you!

 

Out of curiosity, why do I need to begin with

 

{% assign tag_found = product.tags %}
        {% if tag_found != blank %}

 

Wouldn't it work with just:

 

{% if product.tags != blank %}
Savior
Shopify Partner
537 108 161

@earthtoplanet 

 

Sometimes direct comparison doesn't work so we have to assign it to a variable and then compare.

 

Hope your issue has been resolved

 

Please like and accept as solution to help other peoples.

banned
earthtoplanet
Explorer
46 6 8

Thank you!

mjfontec
Shopify Partner
1 0 0

Thank you for this, I have this installed and working on my site.

 

I would also like to create a collection for the 'Product Type' and/or the 'Product Category'. Would you be able to supply the code for that?

 

Thanks,
Mike