Canonical URL field in Blog Posts Collection (for syndicated blog posts)
I have created a Plain Text field called ‘Canonical URL’ in our ‘Blog Posts’ collection on our webflow website. I only set this for a one or two of our blog articles where we are using a syndicated article so we can point back to the canonical original article on the author’s blog using a canonical link.
How I am trying to code this: A canonical link in HTML <head> section ONLY for syndicated blog posts
To do this I am trying to get a rel=“canonical” link into the head area using the custom code section of our blog posts template, but make this conditional i.e. if the field called ‘Canonical URL’ is set then there should be a
in the head section else it should not add anything as the href would be blank if it did. I have tried all sorts of code to try to get this working for several hours now and I have looked through all the help information and forum posts I can find but I can’t find anything that matches this problem. I cannot believe that no one else has this use case as this will be a requirement for anybody that has blog posts that include at least one syndicated post (not least this is vital for SEO so syndication doesn’t look like duplication).
Thanks, I have already done this (except I am using a special field called Canonical URL) is not the issue. The issue is that this whole line needs to be conditional. This whole link line should ONLY appear in the head section for the one or two blog posts that are syndicated and NOT at all for all the other blog posts.
Correct. A blank URL for original content is not acceptable. We need a proper conditional that only puts in the whole line including URL ONLY IF we need a canonical link.
I’m not 100% sure if that would work (as it’s generated only after document is loaded), but this nifty little script I wrote adds that link conditionally:
Should do the trick. The condition here works as following: If you haven’t set any Canonical URL inside of the Canonical Url field in your collection, then it won’t do anything. If you add a link, it would add it after document loads.
Please note that Webflow doesn’t support Custom Code help. If you run into any troubles with this script, feel free to ask on forum.