问题
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