Current tag displaying more than one tag name in Shopify

折月煮酒 提交于 2019-12-11 15:43:52

问题


I am displaying the product tags. If the user clicked on All products filter then It will display only first single tag. If the user clicks on any current tag then it will display on the current tag name.

  {% for tag in product.tags %}
     {% if current_tags contains tag %}
     <a class="link" href="/collections/{{ collection.handle }}">{{ tag | link_to_tag: tag }}</a>

   {% else %}
   <a class="link" href="/collections/{{ collection.handle }}">{{ product.tags[0] | link_to_tag: tag }}</a>
     {% endif %}
   {% endfor %}

Above code is displaying all the tag

My logic is

for(display all tag){
if(clicked current tag){
   display current tag name which is clicked
}
else{
display first tag name
}
}

回答1:


You can achieve it with the following:

{% assign product_tags = products | map: 'tags' %}
     {% if product_tags contains current_tags %}
     <a class="link" href="/collections/{{ collection.handle }}">{{ current_tags | link_to_tag: current_tags }}</a>

   {% else %}
    {% assign first_tag = product.tags | first %}
   <a class="link" href="/collections/{{ collection.handle }}">{{ first_tag | link_to_tag: first_tag }}</a>
     {% endif %}

Try updating this code in your required place and let me know whether you got the result.



来源:https://stackoverflow.com/questions/49707187/current-tag-displaying-more-than-one-tag-name-in-shopify

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!