0

I'm having a lot of problems adding an html tag in head section only on homepage layout (that's defined in my custom theme as page layout). I'm using M2 v. 2.2

In default.xml is defined

<referenceBlock name="head.additional">
    <block class="Magento\Framework\View\Element\Template" name="head.additional.fonts" template="Infortis_Base::html/header/fonts.phtml" after="head.additional.asset-wrapper" />
</referenceBlock>

and I added a block to edit and insert an html tag:

<referenceBlock name="head.additional">
    <block class="Magento\Framework\View\Element\Template" name="head.additional.fonts" template="Infortis_Base::html/header/fonts.phtml" after="head.additional.asset-wrapper" />
    <block class="Magento\Framework\View\Element\Template" name="head.additional.metas" template="Infortis_Base::html/header/metas.phtml" before="head.additional.asset-wrapper" />
</referenceBlock>

so that I would be able to change the phtml template inside the home xml page layout but if I reference the block it doesn't change.

Any help? I'm on this from 2 days and it driving me crazy. THANKS

5
  • 1
    Did you tried with cms_index_index.xml layout file. Commented May 30, 2018 at 14:14
  • @SumitVerma I want the tag only on the homepage layout. I use that layout to add and remove content on the page and those other rules are working fine (like inserting or moving containers)
    – Dany92
    Commented May 30, 2018 at 16:13
  • Can you please share block code in question Commented May 30, 2018 at 16:30
  • the metas.phtml is empty, I've tried inserting a comment and it's showing, but I need to show a custom tag only on the home layout
    – Dany92
    Commented May 30, 2018 at 16:43
  • can you suggest an other way to insert a custom html tag inside the head section in a custom layout xml?
    – Dany92
    Commented May 30, 2018 at 19:24

2 Answers 2

0

You shouldn't use <referenceBlock name="head.additional"> in default.xml. Because it's the place to define head.additional by default. Instead, if you want to show it on homepage only, you should place your code on cms_index_index.xml. So that:

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="head.additional">
            <block class="Magento\Framework\View\Element\Template" name="head.additional.fonts" template="Infortis_Base::html/header/fonts.phtml" after="head.additional.asset-wrapper" />
        </referenceBlock>
    </body>
</page>

Put these code into app/design/frontend/<Vendor_Name>/<Theme_Name>/Magento_Cms/layout/cms_index_index.xml. Then clear cache and refresh. Your block should appear.

2
  • My savior!!!! Thanks a lot! as for <referenceBlock name="head.additional"> in default.xml it was already there...I'm defining a custom child theme. Anyway THANKS <3
    – Dany92
    Commented May 31, 2018 at 6:10
  • 1
    *the body closure has to be </body>
    – Dany92
    Commented May 31, 2018 at 6:11
1

Create New cms_index_index.xml under your theme.

/app/design/frontend/Vendor/YourTheme/Magento_Cms/layout/cms_index_index.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="head.additional">
             <block class="Magento\Framework\View\Element\Template" name="head.additional.fonts" template="Infortis_Base::html/header/fonts.phtml" after="head.additional.asset-wrapper" />
             <block class="Magento\Framework\View\Element\Template" name="head.additional.metas" template="Infortis_Base::html/header/metas.phtml" before="head.additional.asset-wrapper" />
        </referenceContainer>
    <body/>
</page>
3
  • Exactly, the previous answer was right just the path it was in Magento_Cms and not Magento_Theme. Thanks
    – Dany92
    Commented May 31, 2018 at 6:12
  • If my answer working fine vote and accept Commented May 31, 2018 at 6:24
  • Me go first so my answer is accepted. Upvote for you still. XD.
    – PY Yick
    Commented May 31, 2018 at 7:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.