How would I designate a different canonical url in my Shopify blog?

scm22ri
Tourist
4 0 1

I have a blog I use on my Shopify store. I made an agreement with a third party blog to republish some of their content on my blog but they’re requesting their url to be in the meta tag description of my website and not mine, which is fine. My question is, how would I go about customizing my blog canonical url meta tag to show their url and not mine? Thanks 

Replies 6 (6)
ThomasBorowski
Shopify Expert
803 71 230

If you look in your theme's main layout template theme.liquid, you should see this in the <head> section:

<link rel="canonical" href="{{canonical_url}}">

Use something like ShopifyFD to add a metafield to each 3rd-party article that contains the canonical URL of the article. Then change that line above to something like this:

{% if template contains 'article' %}
  {% if article.metafields.metadata.canonical_url %}
    <link rel="canonical" href="{{article.metafields.metadata.canonical_url}}">
  {% else %}
    <link rel="canonical" href="{{canonical_url}}">
  {% endif %}
{% else %}
  <link rel="canonical" href="{{canonical_url}}">
{% endif %}

Adjust the code to the namespace and key you're using for the metafield. And make sure to check that the canonical tags are still being output correctly on all your store's other pages, just to be sure there's no bug in the modified code.

★ Smart Upgrades, Tips and Tutorials for Shopify themes: cartpunk.com
Did my solution work? Help other Community members easily find the correct solution and apply it to their own stores by marking it as the Accepted Solution and giving it a Thumbs Up
scm22ri
Tourist
4 0 1

Update: I editted my syntax below and everything is now working. Below is now my correct syntax.

Hi Thomas,

Thanks for taking the time to answer my question. I understand your logic but I'm not overly familiar with Liquid. I've downloaded ShopifyFD and added my metafields but I can't access the values. 

namespace: mywebsite

key: myurl

value: http://mywebsite.com 

My question is, how do I access the value?

The code:

{% if template contains 'article' %}
  {% if article.metafields.mywebsite.myurl%}
    <link rel="canonical" href="{{article.metafields.mywebsite.myurl}}">
  {% else %}
    <link rel="canonical" href="{{canonical_url}}">
  {% endif %}
{% else %}
  <link rel="canonical" href="{{canonical_url}}">
{% endif %}

 

Jason
Shopify Expert
11119 218 2266

I would explicitly set your conditional statement like this vs assuming a true/false response.

{% if article.metafields.mywebsite.myurl == blank %}
  nothing!
{% else %}
  has something!
{% endif %}

 

★ I jump on these forums in my free time to help and share some insights. Not looking to be hired, and not looking for work. http://freakdesign.com.au ★
scm22ri
Tourist
4 0 1

Jason, thank you for the help 

Below is the correct syntax:

{% if template contains 'article' %}
  {% if article.metafields.mywebsite.myurl == blank %}
    <link rel="canonical" href="{{canonical_url}}">
  {% else %}
    <link rel="canonical" href="{{article.metafields.mywebsite.myurl}}">
  {% endif %}
{% else %}
  <link rel="canonical" href="{{canonical_url}}">
{% endif %}

 

ThomasBorowski
Shopify Expert
803 71 230

Yeah, my bad for not actually testing the code in a theme. Checking for blank instead of true/false is obviously the correct way to do it. Thanks, Jason!

★ Smart Upgrades, Tips and Tutorials for Shopify themes: cartpunk.com
Did my solution work? Help other Community members easily find the correct solution and apply it to their own stores by marking it as the Accepted Solution and giving it a Thumbs Up
scm22ri
Tourist
4 0 1

No, thank you Thomas.