Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
I can see how to add template name and url / handle as a class or id to the body tag, but I'd like to generate and add a unique page id to each page (such as WordPress does) so that even if the page or template name (or url handle) is changed, the selector I've used for that page still works.
Has anyone done this?
Thanks!
Solved! Go to the solution
This is an accepted solution.
Usually, handle does not change even if you change the title of the product/page/article, so there is no real need for this.
But you can also use id -- this would not change even when handle is changed.
Something like (need to add more choices instead of ... )
{% liquid
case request.page_type
when 'product':
assign id = product.id
when 'collection'
assign id = collection.id
when 'blog'
assign id = blog.id
when 'article'
assign id = article.id
when 'page'
assign id = page.id
...
endcase
%}
{% capture uniq_id -%}
{{- request.page_type | split: "/" | last -}}
{%- if template.suffix %}--{{ template.suffix }}{% endif -%}
{%- if id %}--{{ id }}{% endif -%}
{%- endcapture %}
<body class="{{ uniq_id }}">
https://shopify.dev/docs/api/liquid/objects/request
https://shopify.dev/docs/api/liquid/objects/template
https://shopify.dev/docs/api/liquid/objects/product#product-id
Hello @JohnBr ,
You can use this one
<body {% if template.name == "index" %} id="index" {% else %} id="{{ handle }}" {% endif %} class="{{ template }}{% if template == "product" %} {{ product.handle }}{% endif %}{% if template == "collection" %} {{ collection.handle }}{% endif %}">
Regards
Guleria
Yes I tried this, but it didn't produce any result for id on my site. Even if it did, a change of handle would break the selector.
This is an accepted solution.
Usually, handle does not change even if you change the title of the product/page/article, so there is no real need for this.
But you can also use id -- this would not change even when handle is changed.
Something like (need to add more choices instead of ... )
{% liquid
case request.page_type
when 'product':
assign id = product.id
when 'collection'
assign id = collection.id
when 'blog'
assign id = blog.id
when 'article'
assign id = article.id
when 'page'
assign id = page.id
...
endcase
%}
{% capture uniq_id -%}
{{- request.page_type | split: "/" | last -}}
{%- if template.suffix %}--{{ template.suffix }}{% endif -%}
{%- if id %}--{{ id }}{% endif -%}
{%- endcapture %}
<body class="{{ uniq_id }}">
https://shopify.dev/docs/api/liquid/objects/request
https://shopify.dev/docs/api/liquid/objects/template
https://shopify.dev/docs/api/liquid/objects/product#product-id
June brought summer energy to our community. Members jumped in with solutions, clicked ...
By JasonH Jun 5, 2025Learn how to build powerful custom workflows in Shopify Flow with expert guidance from ...
By Jacqui May 7, 2025Did You Know? May is named after Maia, the Roman goddess of growth and flourishing! ...
By JasonH May 2, 2025