簡體   English   中英

如果產品標簽包含 - 在 shopify

[英]If product tags contains - in shopify

因此,如果產品的標簽包含“相關”字樣,我基本上是在嘗試使用 shopify 的邏輯來顯示圖像

(有一個 json 查詢獲取包含“related-x”的標簽,其中 x 是產品名稱,它使用它來顯示相關產品。)

在 Json 查詢之前是一個基本上說“相關產品”的圖像。 我想做的是僅在存在“相關”標簽時才顯示此內容。

我試過這個:

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

哪個不顯示任何內容。 我也試過:

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

但是,每次查詢返回相關產品時,這都會顯示圖像。

我追求的是 go (圖像)(查詢結果) - 如果沒有查詢結果,那么它什么也不顯示。

有任何想法嗎?

您的第一段代碼不起作用的原因是因為contains正在尋找一個名為“相關”的標簽,而不是包含 substring“相關”的標簽。

請參閱Shopify Wiki,了解其中包含的內容:

它可以檢查另一個字符串中是否存在字符串,也可以檢查簡單字符串數組中是否存在字符串。

在您的實例中, contains正在檢查簡單字符串數組中的字符串(並且正在查找整個字符串,而不是包含指定字符串作為子字符串的字符串)。

另請參閱Shopify wiki 了解 product.tags

返回產品標簽列表(由​​簡單字符串表示)。

您可以將 contains 關鍵字與簡單字符串數組一起使用,因此您可以將其與產品標簽一起使用:

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

因此, Gerard Westerhof建議在上面的評論中使用Join是一個很好的建議。 如果您首先加入product.tags數組,則contains將在join返回的標簽字符串中搜索“相關”字符串。

嘗試這個:

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

編輯:

試試這個:

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