简体   繁体   English

如果产品标签包含 - 在 shopify

[英]If product tags contains - in shopify

So i'm basically trying to use the logic of shopify to show an image if the tags of the product contains the words "related"因此,如果产品的标签包含“相关”字样,我基本上是在尝试使用 shopify 的逻辑来显示图像

(there is a json query that picks up the tags containing 'related-x' where x is the name of the product and it uses this to show the related products.) (有一个 json 查询获取包含“related-x”的标签,其中 x 是产品名称,它使用它来显示相关产品。)

Preceding the Json query is an image that says "related products" basically.在 Json 查询之前是一个基本上说“相关产品”的图像。 what i would like to do is to only display this when there are "related" tags present.我想做的是仅在存在“相关”标签时才显示此内容。

I have tried this:我试过这个:

{% if product.tags contains 'related' %}              
          <img src="{{ 'complete-this-look.gif' | asset_url }}" alt="" align="left;" style="vertical-align:top; margin-right:8px; padding-top:0px;" width="130" height="175"/>
{% endif %}

Which doesnt display anything.哪个不显示任何内容。 I also tried:我也试过:

{% for t in product.tags %}
{% if t contains 'related-' %}
<img src="{{ 'complete-this-look.gif' | asset_url }}" alt="" align="left;" style="vertical-align:top; margin-right:8px; padding-top:0px;" width="130" height="175"/>
{% endif %}
{% endfor %}

However this will display the image every time a related product is returned by the query.但是,每次查询返回相关产品时,这都会显示图像。

What im after is for it to go (Image) (Query Results) - and if there is no query results then it displays nothing.我追求的是 go (图像)(查询结果) - 如果没有查询结果,那么它什么也不显示。

Any ideas?有任何想法吗?

The reason your first piece of code is not working is because contains is looking for a tag called 'related', not a tag containing the substring 'related'.您的第一段代码不起作用的原因是因为contains正在寻找一个名为“相关”的标签,而不是包含 substring“相关”的标签。

See the Shopify Wiki for contains where it states:请参阅Shopify Wiki,了解其中包含的内容:

It can check for the presence of a string in another string, or it can check for the presence of a string in an array of simple strings.它可以检查另一个字符串中是否存在字符串,也可以检查简单字符串数组中是否存在字符串。

In your instance, contains is checking for a string in an array of simple strings (and is looking for the whole string, not a string containing the specified string as a substring).在您的实例中, contains正在检查简单字符串数组中的字符串(并且正在查找整个字符串,而不是包含指定字符串作为子字符串的字符串)。

See also the Shopify wiki for product.tags :另请参阅Shopify wiki 了解 product.tags

Returns a list of the product's tags (represented by simple strings).返回产品标签列表(由​​简单字符串表示)。

You can use the contains keyword with an array of simple strings, so you can use this with a product's tags:您可以将 contains 关键字与简单字符串数组一起使用,因此您可以将其与产品标签一起使用:

{% if product.tags contains 'custom to order' %}
< -- Output custom to order form HTML here -->
{% endif %}

So, Gerard Westerhof 's suggestion to use Join in the comment above is a good one.因此, Gerard Westerhof建议在上面的评论中使用Join是一个很好的建议。 If you first join the product.tags array, then contains will search for the 'related' string inside the string of tags returned by join .如果您首先加入product.tags数组,则contains将在join返回的标签字符串中搜索“相关”字符串。

Try this:尝试这个:

{% if product.tags | join: ' ' contains 'related' %}
    <img src="{{ 'complete-this-look.gif' | asset_url }}" alt="" align="left;" style="vertical-align:top; margin-right:8px; padding-top:0px;" width="130" height="175"/>
{% endif %}

EDIT:编辑:

Try this instead:试试这个:

{% assign product_tags_string = product.tags | join: ' ' %}
{% if product_tags_string contains 'related' %}
    <img src="{{ 'complete-this-look.gif' | asset_url }}" alt="" align="left;" style="vertical-align:top; margin-right:8px; padding-top:0px;" width="130" height="175"/>
{% endif %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM