【问题标题】:Current tag displaying more than one tag name in Shopify当前标签在 Shopify 中显示多个标签名称
【发布时间】:2018-04-07 12:03:15
【问题描述】:

我正在显示产品标签。如果用户点击All products filter 那么它将只显示第一个标签。如果用户点击任何当前标签,那么它将显示在当前标签名称上。

  {% 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 %}

以上代码显示所有标签

我的逻辑是

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

【问题讨论】:

  • 是的,否则部分不起作用。我的意思是它正在显示所有标签。我只需要单个标签或第一个标签

标签: if-statement tags shopify product


【解决方案1】:

您可以通过以下方式实现它:

{% 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 %}

尝试在您需要的地方更新此代码,让我知道您是否得到了结果。

【讨论】:

  • 当前标签存在一些问题 Liquid 错误(product-card-grid 第 6 行):标签过滤器参数必须是字符串或标签
  • 其他部分正在工作,但是当我点击标签时,当前标签没有显示错误
  • 我只有三个标签,分别是苹果、芒果、香蕉。所以我点击了苹果,我得到了一个错误
  • 给我10分钟,我会和你分享
  • 您必须点击collection_1_images,您将在产品图片顶部获得两个产品,您将获得第一个正确的标签名称。现在单击过滤器部分中的 kivi_tag,您将获得两个产品,但这次标签名称是 kivi_2_tag 和 kivi_1_tag。我需要 kivi_tag 图像
猜你喜欢
  • 1970-01-01
  • 2014-12-28
  • 1970-01-01
  • 1970-01-01
  • 2016-07-15
  • 2017-03-01
  • 2019-12-23
  • 2015-05-14
  • 2017-01-17
相关资源
最近更新 更多