2014-03-04 56 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没有问题,很高兴它工作:) –