【问题标题】:how to properly query in collections page in shopify liquid?如何在shopify液体的收藏页面中正确查询?
【发布时间】:2020-05-22 09:13:26
【问题描述】:

我创建了许多系列,例如 All ProductsNew ReleasesT 恤裤子等。我有两类产品,例如 男士女士。一些专为男装设计的系列和一些专为女装设计的系列。我创建了两个标签 Men'sWomen's。我通过collections 查询tags 创建menu。我的我的产品集合url 是这样的:

/collections/all-products/mens
/collections/all-products/womens
/collections/new-releases/mens
/collections/new-releases/womens
/collections/bras/womens

我想在collection.url 显示/mens/womens 时显示一些文本和菜单列表。

{% if collection.url contains mens %}
    do something
{% endif %

上述条件不起作用。我知道为什么不工作,因为{{ collection.url }} 提供/collections/all-products{{ page.url }} 不适用于收集 object。我没有找到任何建议或液体代码参考,其中在集合页面中显示男士或女士产品会显示特定文本。

如果在loop 中使用它会起作用。

{% for product in collection.products %}
    {% for tag in product.tags %}
        {% if tag contains 'mens' %}
            <h3>Mens Products</h3>
        {% endif %}
    {% endfor %}
{% endfor %}

上面的代码对我不起作用,因为它在loop 中。我需要在loop 之外显示。我不明白如何实现它。这是reference site。下面有我想要的图像。

需要帮助!

【问题讨论】:

  • man = 单数 men = 复数 mens... 不存在

标签: shopify liquid


【解决方案1】:

您可以访问current_tags,请参阅文档:https://shopify.dev/docs/themes/liquid/reference/objects/current-tags

这将返回您当前正在查看的所有标签的数组(如果您正在查看多个标签)。

所以你的支票是:

{% if current_tags contains 'mens' %}
    do something
{% endif %}

差不多就是这样。

【讨论】:

  • 感谢您的回答。在问这个问题之前,我检查了很多次以上的参考,但没有工作。我想通了。 {% for tag in collection.all_tags %}{% if current_tags contains tags %}{% if tag == 'mens' %} 做点事情{% endif %}{% endif %}{% endfor %}。它对我有用。我错了吗?
  • @Rahul 你和我做同样的事情,但是你将它插入一个循环中,如果你的解决方案有效,那么提供的解决方案没有理由不起作用。如果您的标签不称为 mens 而是其他名称,那么它不起作用的唯一原因。
【解决方案2】:

我真的很喜欢菜单的呈现方式!如果您还没有找到您想要的地方,这里有一些您可以为您的类别特定菜单考虑的想法。

  1. 对于您的 url 策略,您正在寻找的是 handle。手柄专用于液体。 https://shopify.dev/docs/themes/liquid/reference/basics/handle

  2. 如果这两个类别需要完全不同,您可以制作自定义集合模板:https://shopify.dev/tutorials/customize-theme-create-alternate-templates。如果你这样做,那么你可以使用集合对象中的template_prefix

  3. 在循环外分配一个变量,然后在循环内设置它:

    {% assign is_mens = false %} {% for tag in product.tags %} {% if tag contains 'mens' %} {% assign is_mens = true %} {% endif %} {% endfor %}

然后 {% if is_mens %} 或 {% unless is_mens %} 用于您的动态内容,或用于定义特定于菜单中类别的内容的案例语句。

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多