Add Meta Tag Based on Layout in Magento 2 – How to Guide

headlayoutmagento2

I have 3 main categories in my stores. In order to give them a general color theme, I created 3 layouts file in app/design/Vendor/Theme/Magento_Theme/page_layout, they are named category_white , category_black , category_blue

Here is one of them (all 3 have same content actually)

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
    <update handle="1column"/>

</layout>

This them gives me an additional class on my <body> and I use it in my CSS to give the proper colors.

When it comes to page building, my page configuration files always refer to layout="1column" then my 3 layouts inherit from it.

Now, I want as well to add the tag : <meta name="theme-color" content="#000000" /> (with of course a different content for each of the 3 categories), but I understood that I can not do it directly from the layout files I have to do it from the page configuration files (i.e. not from page_layout folder but from layout folder).

How can I target only one layout, I tried to have my default.xml as below and it does not work.

<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
...
</page>
<page layout="category_white" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
...
</page>

Best Answer

You can add as in HTML:

your Layout would look like-

<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd">
  <head>
    <meta name="description" content=" "/>
  </head>
.......
.......
</layout>
Related Topic