In Magento 2, I want to add custom cms page link in header menu before wishlist link.
Please tell me how we can add custom page link before wishlist link?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityIn Magento 2, I want to add custom cms page link in header menu before wishlist link.
Please tell me how we can add custom page link before wishlist link?
You have to add bellow code to in your theme default.xml
<referenceBlock name="top.links">
<block class="Magento\Framework\View\Element\Html\Link\Current" name="cms-page-name" before="wish-list-link">
<arguments>
<argument name="label" xsi:type="string">cms-page-name</argument>
<argument name="path" xsi:type="string">cms-page-path</argument>
</arguments>
</block>
</referenceBlock>
Clean cache and check.
Let me know if you have any difficulty.
you have to just override template file for wishlist into your theme.
path,
app/design/frontend/Vendorname/themename/Magento_Wishlist/templates/link.phtml
Now you can write down your cms page link code before wishlist code, this tag.
<?php $link = 'Cms page link'; ?>
<div class="link cmspage">
<a <?php echo $link; ?>>CMS Page</a>
</div>
In Namespace/ModuleName/view/frontend/layout/default.xml
<?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>
<referenceContainer name="header.container">
<block class="Magento\Framework\View\Element\Html\Links" name="top.links">
<arguments>
<argument name="css_class" xsi:type="string">header links</argument>
</arguments>
<block class="Namespace\ModuleName\Block\Links\Link" name="linkname" before='wish-list-link'/>
</block>
</referenceContainer>
</body>
Namespace\ModuleName\Block\Links\Link.php
<?php
namespace Namespace\ModuleName\Block\Links;
class Link extends \Magento\Framework\View\Element\Html\Link
{
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
array $data = []
) {
parent::__construct($context, $data);
}
public function getHref(){
$page_url = '[cms url key]';
return $this->getUrl($page_url);
}
public function getLabel(){
return '[Link Name]';
}
}