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

Highlighted
New Member
4 0 0

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 

0 Likes
Highlighted
Shopify Expert
539 24 106

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.

★ ThemeUp: Nifty Upgrades for your Shopify Store ★ https://themeup.net
1 Like
Highlighted
New Member
4 0 0

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 %}

 

0 Likes
Highlighted
Shopify Expert
9852 98 1614

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 %}

 

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
1 Like
Highlighted
New Member
4 0 0

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 %}

 

0 Likes
Highlighted
Shopify Expert
539 24 106

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!

★ ThemeUp: Nifty Upgrades for your Shopify Store ★ https://themeup.net
0 Likes
Highlighted
New Member
4 0 0

No, thank you Thomas. 

0 Likes