You need to enable JavaScript to run this app. Shopify Liquid中如何为多个页面设置template判断条件
优惠活动
大模型
产品
解决方案
定价
更多

Shopify Liquid中如何为多个页面设置template判断条件

调整方法

如果你只需要适配首页+商品详情页2个页面,直接用Liquid的逻辑或运算符 or 拼接判断条件即可,修改后代码如下:

{% if template.name == "index" or template.name == "product" %} 
 do something
{% endif %}

如果后续需要适配的页面数量较多,更推荐用数组匹配的写法,维护起来更简洁,不需要堆叠大量or判断:

{% comment %} 把所有需要生效的模板名称放到下面的字符串里,用英文逗号分隔 {% endcomment %}
{% assign valid_templates = "index,product,blog,page.contact" | split: "," %}
{% if valid_templates contains template.name %}
  do something
{% endif %}

内容的提问来源于stack exchange,提问作者muhammad shafique

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.21 22:45:03