Magento – How to add canonical or any meta head in Magento 2 via CMS Layout XMLs

layoutmagento2xml

I have a custom CMS page with the url of /test

I tried pasting this in the Layout Update XML under Page Layout AND also tried under the Custom Theme Layout Update XML.. both options do not work

<referenceContainer name="before.body.end">
    <block class="Magento\Cms\Block\Block">
        <arguments>
            <argument name="block_id" xsi:type="string">sale_campaign_canonical</argument>
        </arguments>
    </block>
</referenceContainer>

This sale_campaign_canonical is a static block available on all storefronts and has

<link rel="canonical" href="/test" />

Inside of it

Thanks

Best Answer

Use "widgets"

enter image description here

content > widgets > Add Widget | Type: CMS Block / Design Theme (Your Design)

enter image description here

Then, in storefront properties, you can choose where you want it to be/load "all Pages" or "All Product Pages" and then select "Page Header".

enter image description here

Then select your CMS Block

enter image description here

The new widget functions in Magento 2 are really helpful for putting CMS Blocks where you need. Meta tags, scripts, static information or just about anything. It's really simple, easy to use and you can turn it on or off at will. I used it to add microdata meta tags for condition as well as adding product tabs to certain products/categories. It's very versatile.