How to create custom header Link any one suggest me thanks in advance.I used many tutorial but does not work for me.
3 Answers
Add below code
app/design/frontend/Vendor/YourTheme/Magento_Theme/layout/default.xml
<referenceBlock name="header.links">
<block class="Magento\Framework\View\Element\Html\Link\Current" name="custome-link" >
<arguments>
<argument name="label" xsi:type="string">your-label</argument>
<argument name="path" xsi:type="string">your-link-path</argument>
</arguments>
</block>
</referenceBlock>
First, configure and register your custom module. Then create default.xml file in app/code/Magenticians/Mymodule/view/frontend/layout and paste the following code in it:
<?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>
<referenceBlock name="header.links">
<block class="Magenticians\Mymodule\Block\Link" name="custom-header-link">
<arguments>
<argument name="label" xsi:type="string" translate="true">Contact Us</argument>
<argument name="path" xsi:type="string" translate="true">contact</argument>
</arguments>
</block>
</referenceBlock>
</body>
</page>
Now Create Link.php file in app/code/Magenticians/Mymodule/Block and paste the following code in it:
<?php
namespace Magenticians\Mymodule\Block;
class Link extends \Magento\Framework\View\Element\Html\Link
{
/**
* Render block HTML.
*
* @return string
*/
protected function _toHtml()
{
if (false != $this->getTemplate())
{
return parent::_toHtml();
}
return '<li><a ' . $this->getLinkAttributes() . ' >' . $this->escapeHtml($this->getLabel()) . '</a></li>';
}
}
At last just run these commands:
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:clean
php bin/magento cache:flush
you achieve this using default.xml ( Try below code )
<?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>
<referenceBlock name="header.links">
<block class="Magento\Framework\View\Element\Html\Link\Current" name="name-of-your-header-link">
<arguments>
<argument name="label" xsi:type="string" translate="true">Custom Header Link</argument>
<argument name="path" xsi:type="string">path of your header link</argument>
</arguments>
</block>
</referenceBlock>
</body>
</page>
-
<?xml version="1.0"?> <config xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="ProgrammingAtKstark_CatewithimgWidget" setup_version="1.0.0"> </module> </config> Commented Aug 23, 2017 at 5:41
-
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Dckap_Newlink', DIR ); Commented Aug 23, 2017 at 5:42
-
<?xml version="1.0"?> <config xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd"> <router id="standard"> <route id="newlink" frontName="newlink"> <module name="Dckap_Newlink" /> </route> </router> </config> Commented Aug 23, 2017 at 5:42
-
<?xml version="1.0"?> <page xmlns:xsi="w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd"> <body> <referenceBlock name="header.links"> <move element="test-link" destination="header.links"/> <block class="Dckap\Newlink\Block\Header" name="test-link" after="my-account-link"/> </referenceBlock> </body> </page> Commented Aug 23, 2017 at 5:42
-
<?php namespace Dckap\Newlink\Block; class Header extends \Magento\Framework\View\Element\Html\Link { protected $_template = 'Dckap_Newlink::link.phtml'; public function getHref() { return__( 'testuser'); } public function getLabel() { return __('Test Link'); } } ?> Commented Aug 23, 2017 at 5:42