我正在尝试检查我的当前页面是否是一个集合页面,而不是某个集合中的单个产品页面。
我的意思是,例如,如果有人进入鞋子的集合页面,那么我可以使用 collection.handle == 'Shoes' 进行检查,但是如果我从该页面选择一个产品,那么它仍然会给我 true 但我希望我的条件为 true如果是收藏页面则打开。 感谢您的帮助!
template
:
{% if template contains 'collection' %}
Do something
{% endif %}
随着 Shopify 的发展,您现在可以使用它:
{% if template.name == 'collection' %}
Do something
{% endif %}
避免模板命名错误的罕见情况的更安全替代方案是使用
request.page_type
- 请参阅 Shopify 文档
{% if request.page_type == 'collection' %}
Do something
{% endif %}
我会用:
{% if template == 'collection' %}
Do something
{% endif %}
因为如果您使用
contains
而不是 ==
(等于),如果您创建了包含单词集合的新模板,您可能会面临更改的风险?
有条件的可以尝试一下。
{%if request.page_type == "collection" %}
--- True if visit collection page or sub collection page. ---
{% endif %}
谢谢。
对于定义站点范围的页面类型,我喜欢这样:
<body _pagetype="{{ request.page_type | handle }}">
由于模板子集有一个“.”,例如 collection.xyz、page.xyz、product.xyz 我将使用此代码
{%- assign t = template | split: '.' | first -%}
{%- if t == 'page' -%}
{%- endif -%}
{%- if t == 'product' -%}
{%- endif -%}
{%- if t == 'collection' -%}
{%- endif -%}