Adding a new custom phtml block to the header
-
03-10-2020 - |
Question
How does one add a new block and template file to the header in Magento2?
So far I've created a child theme, which is working, including a default.xml
, which is successfully moving other block around. But for creating a new block it won't work.
So far I have:
<?xml version="1.0" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<move element="top.links" destination="header-wrapper" before="-" />
<referenceContainer name="header-wrapper">
<container name="custom.header" htmlClass="headerSitemenu" htmlTag="div">
<block class="Magento\Framework\View\Element\Template" name="navigation.sections" before="-" template="Magento_Theme::html/headersitemenu.phtml">
</block>
</container>
</referenceContainer>
and have created headersitemenu.phtml
inside <theme>/Magento_Theme/html
.
Flushed and cleaned the cache etc, but nothing shows.
Solution
wrong template file location it should be in
<theme>/Magento_Theme/templates/html/headersitemenu.phtml
Update
block name navigation.sections already exist so you can't use change to some other name, block name should be unique. I reckon enable developer mode to get error details. logs are placed in <magento-root>/var/log/*
check there you got any error related to invalid template.
if you would like to customize existing block, change <block .....
to <referenceBlock name="navigation.sections">