Liquid, JavaScript, themes, sales channels
Hi,
We are trying to return the true canonical value instead of an url with a search parameter.
So instead of having something like this:
[domain]/collections/jackets-coats?page=srnqqdjnmld
We would like to show something like this:
[domain]/collections/jackets-coats
Does anyone knows why the following code returns an empty string?
{% assign canonicalUrl = {{ canonical_url }} %}
{% assign secondUrl = canonicalUrl | split: '?' | last %}
{% if secondUrl != 'page=2' %}
{% assign canonicalUrl = canonicalUrl | split: '?' | first %}
{% endif %}
<link rel="canonical" href="{{ canonicalUrl }}" />
Code above works in a liquid sandbox.
Thanks.
Cheers,
Gonkas
Solved! Go to the solution
This is an accepted solution.
Hi @Gonkas,
Please change code:
{% assign canonicalUrl = canonical_url %}
{% assign secondUrl = canonicalUrl | split: '?' | last %}
{% if secondUrl != 'page=2' %}
{% assign canonicalUrl = canonicalUrl | split: '?' | first %}
{% endif %}
<link rel="canonical" href="{{ canonicalUrl }}" />
Hope it helps!
This is an accepted solution.
The issue is extra {{ }} {% assign canonicalUrl = {{ canonical_url }} %}
This is the correct code: {% assign canonicalUrl = canonical_url %}
But the canonical URL is the page's "default" URL with any URL parameters removed so nothing more needs to be done.
For example, for a product in a collection with a variant selected:
https://example.com/collections/classics/products/shoes?variant=17287731270
The canonical URL is the product page: https://example.com/products/shoes
Have a great day!!!
- Was I helpful? Like to let me know 🙂
- Did I answer your question? Mark my reply as Accepted Solution
This is an accepted solution.
Hi @Gonkas,
Please change code:
{% assign canonicalUrl = canonical_url %}
{% assign secondUrl = canonicalUrl | split: '?' | last %}
{% if secondUrl != 'page=2' %}
{% assign canonicalUrl = canonicalUrl | split: '?' | first %}
{% endif %}
<link rel="canonical" href="{{ canonicalUrl }}" />
Hope it helps!
Shouldn't it be:
{% if secondUrl == "page=2" %}
Your code doesn't work for me, but after making that change it does.
This is an accepted solution.
The issue is extra {{ }} {% assign canonicalUrl = {{ canonical_url }} %}
This is the correct code: {% assign canonicalUrl = canonical_url %}
But the canonical URL is the page's "default" URL with any URL parameters removed so nothing more needs to be done.
For example, for a product in a collection with a variant selected:
https://example.com/collections/classics/products/shoes?variant=17287731270
The canonical URL is the product page: https://example.com/products/shoes
Have a great day!!!
- Was I helpful? Like to let me know 🙂
- Did I answer your question? Mark my reply as Accepted Solution
Thanks both, it was extra {{ }}.
Shopify should work that out but it doesn't so needs this "hack".
Have a great day!
Are you ready to take your business to the next level? Look no further than the latest ...
By SarahF_Shopify Apr 15, 2024We’re keeping the ball rolling to make sure you’re always ahead of the game. So buckle ...
By JasonH Apr 8, 2024Portrait of Stephen positioned next to an image of planet Earth, with the Stephen's World ...
By JasonH Mar 18, 2024