【问题标题】:shopify I need to check if current page is a collection page and not a single product pageshopify 我需要检查当前页面是否是集合页面而不是单个产品页面
【发布时间】:2016-10-08 12:11:17
【问题描述】:

我正在尝试检查我的当前页面是否是集合页面而不是某个集合中的单个产品页面。

我的意思是,例如,如果有人进入鞋子的收藏页面,那么我可以使用 collection.handle == 'Shoes' 进行检查,但如果我从该页面选择产品,那么它仍然会给我真实的但我希望我的条件如果它是收藏页面,则为 true。 感谢您的帮助!

【问题讨论】:

    标签: shopify liquid liquid-layout dotliquid


    【解决方案1】:

    使用template这个简单的方法:

    {% if template contains 'collection' %}
        Do something
    {% endif %}
    

    随着 Shopify 的发展,您现在可以使用它:

    {% if template.name == 'collection' %}
        Do something
    {% endif %}
    

    【讨论】:

    • 如果您需要检查您是否在产品页面上,如果模板包含“产品”,也可以使用。
    • 小心...任何名称包含“collection”的产品都将为此注册为 true。诚然,这种机会很少见,但它就在那里。
    • 绝对不是。变量检查模板名称。因此,如果您在产品页面上,则变量包含 «product»。即使 {{ product.name }} 包含单词 Collection,这与模板名称无关。
    • 我想我明白了。我把它与位置href混淆了。太棒了,谢谢
    【解决方案2】:

    避免模板命名错误的罕见情况的更安全的替代方法是使用request.page_type - 请参阅the Shopify docs

    {% if request.page_type == 'collection' %}
      Do something
    {% endif %}
    

    【讨论】:

      【解决方案3】:

      我会使用:

      {% if template == 'collection' %}
        Do something
      {% endif %}
      

      因为如果您使用contains 而不是==(等于),如果您曾经创建过包含单词集合的新模板,您可能会面临更改的风险?

      【讨论】:

        【解决方案4】:

        有条件的可以试试这个。

        {%if request.page_type == "collection" %} 
        --- True if visit collection page or sub collection page. ---
        {% endif %}
        

        谢谢。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-10
          • 1970-01-01
          • 2022-11-23
          • 1970-01-01
          • 2011-07-13
          • 1970-01-01
          相关资源
          最近更新 更多