2014-03-04 53 views
0

我正嘗試在電子商務商店中爲我的產品頁面添加麪包屑。我需要一些液體語法的幫助。液體中的麪包屑

我想包括第一個產品標籤作爲麪包屑的一部分,除非標籤是'貼紙','信紙'或'配件',在這種情況下,我想使用第二個標籤。如果第二個標籤也是「貼紙」,「文具」或「配件」,我想使用第三個標籤,依此類推。

也許更好的方式來說這將是:我想打電話給第一個可用的標籤,不是'貼紙','文具',或'配件'。

我已經得到最接近的是這樣的:

{% if product.tags.first contains 'stickers' or product.tags.first contains 'stationery' or product.tags.first contains 'accessories' %} 
<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="/collections/all/{{ product.tags.last }}" itemprop="url"><span itemprop="title">{% if product.tags.size > 0 %}{% assign words = product.tags.last | split: '-' %}{% for word in words %}{% if word == 'and' %}{{ word }} {% else %}{{ word | capitalize }} {% endif %} {% endfor %}{% endif %}</span></a></li> 
{% else %} 
<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="/collections/all/{{ product.tags.first }}" itemprop="url"><span itemprop="title">{% if product.tags.size > 0 %}{% assign words = product.tags.first | split: '-' %}{% for word in words %}{% if word == 'and' %}{{ word }} {% else %}{{ word | capitalize }} {% endif %} {% endfor %}{% endif %}</span></a></li> 
{% endif %} 

這是不雅,但它的工作原理,直到一個點。第一個標籤和最後一個標籤都屬於我想要排除的類別之一。

據我所知,似乎沒有一個選項可以調用像「product.tags.second」(只有「第一」和「最後」似乎工作)。

我有點超出了我的深度,真的很感謝一些關於如何解決這個問題的建議。

我在的平臺是Shopify。

謝謝!

+1

你不能只在產品標籤複製到其他一些變量,並刪除您所談論的三個標籤,然後就顯示,截至麪包屑? –

+0

聽起來好像是邏輯上的聲音 - 但是,我不知道如何去做你的建議 – Ben

+0

只需要補充,下面Steph發佈的解決方案概述瞭如何實現你的建議。乾杯。 – Ben

回答

3

這裏有幾個方法,你可以接近它:

<!-- 1. Loop through the product tags to find the first tag you want included in the breadcrumb --> 
{% assign found_tag = false %} 
{% assign tag_for_breadcrumb = '' %} 

{% for tag in product.tags %} 
    {% if found_tag == false and tag != 'stickers' and tag != 'stationery' and tag != 'accessories' %} 
    {% assign tag_for_breadcrumb = tag %} 
    {% assign found_tag = true %} 
    {% endif %} 
{% endfor %} 

{{ tag_for_breadcrumb }} 

<!-- 2. Convert the tags array into a string, remove the tags you don't want, and then get the first tag from those remaining --> 
{% assign tag_string = product.tags | join: ' ' %} 
{% assign filtered_tag_string = tag_string | remove: 'stickers' | remove: 'stationery' | remove: 'accessories' %} 
{% assign filtered_tag_array = filtered_tag_string | split: ' ' %} 
{{ filtered_tag_array.first }} 
+0

輝煌。我實施了第一個建議,並且它工作。謝謝。 – Ben

+1

@Ben沒有問題,很高興它工作:) –