如何修改theme.liquid代码,为所有页面设置SEO友好的合适标题
为Shopify所有页面设置符合SEO的动态标题
直接替换你现有<title>标签的代码为以下内容,覆盖所有页面场景,同时遵循SEO标题长度(50-60字符)的最佳实践:
<title> {% comment %} 首页:优先用店铺自定义SEO标题,否则组合店铺名+描述 {% endcomment %} {% if template == "index" %} {{ shop.metafields.seo.title | default: shop.name | append: " | " | append: shop.description | truncate: 60, "" }} {% comment %} 产品页:保留原有逻辑,突出产品核心 {% endcomment %} {% elsif template == "product" %} {{ product.title | append: " | " | append: shop.name | truncate: 60, "" }} {% comment %} 集合页:展示分类名称,明确页面定位 {% endcomment %} {% elsif template == "collection" %} {{ collection.title | append: " | " | append: shop.name | truncate: 60, "" }} {% comment %} 博客文章页:优先展示文章标题,强化内容辨识度 {% endcomment %} {% elsif template == "article" %} {{ article.title | append: " | " | append: blog.title | append: " | " | append: shop.name | truncate: 60, "" }} {% comment %} 博客列表页:展示博客名称,清晰内容归属 {% endcomment %} {% elsif template == "blog" %} {{ blog.title | append: " | " | append: shop.name | truncate: 60, "" }} {% comment %} 自定义页面(如关于我们):直接用页面标题 {% endcomment %} {% elsif template == "page" %} {{ page.title | append: " | " | append: shop.name | truncate: 60, "" }} {% comment %} 购物车/结账页:固定名称,明确功能 {% endcomment %} {% elsif template == "cart" %} {{ "购物车" | append: " | " | append: shop.name }} {% elsif template == "checkout" %} {{ "结账" | append: " | " | append: shop.name }} {% comment %} 兜底逻辑:处理所有未覆盖的页面,避免标题为空 {% endcomment %} {% else %} {{ page_title | default: shop.name | truncate: 60, "" }} {% endif %} </title>
代码说明
- 针对性适配不同页面模板,每个场景的标题都符合用户和搜索引擎的认知习惯
- 用
truncate控制标题长度在60字符内,避免被搜索引擎截断 - 保留你原有产品页的逻辑,同时补充了首页、集合页、博客、自定义页面等全场景覆盖
- 默认分支兜底,确保任何页面都不会出现空标题
如果你的主题支持自定义页面的SEO标题字段(比如页面设置里的「Meta Title」输入框),可以把对应分支的代码改成{{ page.metafields.seo.title | default: page.title | append: " | " | append: shop.name }},优先使用你手动设置的SEO标题,灵活性更高。
内容的提问来源于stack exchange,提问作者crunchmouth
相关产品推荐
相关产品推荐

