0

My Magento version is 2.3, I want to create "home" and "about" link in the category, I search few articles but it is for Magento 2.0 version, I was not able to find for Magento 2.3 version. I want first Home, About then categories.

https://www.marketplace.jawadditaksal.org/

  • No it is not working as it says Create registration.php file and add the file to - app/code/Magenik/NavLink/registration.php but in the 2.3 version there is no "code" folder in the "app" folder. – winnersingh Jan 1 '19 at 13:53
  • You have to need create same folder if not exists. – Kirti Nariya Jan 1 '19 at 14:17
  • I follow the article, but nothing work for me. – winnersingh Jan 9 '19 at 13:30
  • @KirtiNariya can you please suggest me for 2.3, non of article work for me – winnersingh Jan 14 '19 at 13:25
0

the lines below in a template that suits where you want these links to appear will show 2 links to go to About us page and to Home page

<a href="<?php echo $block->getUrl('about-us'); ?>"><?php echo __('About'); ?></a>
<a href="<?php echo $block->getUrl(''); ?>"><?php echo __('Home'); ?></a>
  • Where exactly I need to add these code ? – winnersingh Jan 15 '19 at 12:25
  • Sorry mate, I retired from Magento stackexchange until we use a better reviewing system. Good luck – Herve Tribouilloy Jan 15 '19 at 17:42
0

you don't have to create any category for this.

You need to add this link via code. At Magento, the menu link has been generated from Magento\Theme\Block\Html\Topmenu and _getHtml() is give main content of the menu.

So, create pluginon this class and use before plugin over _getHtml()

1. create di.xml where we will define the plugin. File location app\code{VendorName}{ModuleName}\etc\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Theme\Block\Html\Topmenu">
        <plugin name="add_cms_menu" type="{VendorName}{ModuleName}\Plugin\Topmenu" sortOrder="1" />
    </type>
</config>

2. Assume that about us as cms page.Then create the plugin class where we will add a cms page link to the menu.

  <?php 
namespace {VendorName}\{ModuleName}\Plugin;
use Magento\Framework\Data\Tree\NodeFactory;
class Topmenu
{
    protected $nodeFactory;
    protected $_storeManager;
    protected $_pageFactory;
    protected $_urlBuilder;

    public function __construct(
        NodeFactory $nodeFactory,
        \Magento\Cms\Model\PageFactory $pageFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\UrlInterface $urlBuilder
    ) {
        $this->nodeFactory = $nodeFactory;
        $this->_pageFactory = $pageFactory;
        $this->_storeManager = $storeManager;
        $this->_urlBuilder = $urlBuilder;
    }
    public function beforeGetHtml(
        \Magento\Theme\Block\Html\Topmenu $subject,
        $outermostClass = '',
        $childrenWrapClass = '',
        $limit = 0
    ) {

        $node = $this->nodeFactory->create(
            [
                'data' => [
                    'name' => __('Home')
                    'id' =>  ''home,
                    'url' =>  $this->_urlBuilder->getUrl(),
                    'has_active' => false,
                    'is_active' => false // (expression to determine if menu item is selected or not)
                ],
                'idField' =>  -10,
                'tree' => $subject->getMenu()->getTree()
            ]
        );
        $subject->getMenu()->addChild($node);
    }
        /* Showing  Cms page About us at menu */
        $page = $this->getCmspage('about-us');
        if($page == null){
            return;
        }


        $node = $this->nodeFactory->create(
            [
                'data' => [
                    'name' => $page->getTitle(),
                    'id' => $page->getIdentifier(),
                    'url' =>  $this->_urlBuilder->getUrl(null, ['_direct' => $page->getIdentifier()]),
                    'has_active' => false,
                    'is_active' => false // (expression to determine if menu item is selected or not)
                ],
                'idField' => -9,
                'tree' => $subject->getMenu()->getTree()
            ]
        );
        $subject->getMenu()->addChild($node);
    }
    protected function getCmspage($identifier){

        $page = $this->_pageFactory->create();
        $pageId = $page->checkIdentifier($identifier, $this->_storeManager->getStore()->getId());

        if (!$pageId) {
            return null;
        }
        $page->setStoreId($this->_storeManager->getStore()->getId());
        if (!$page->load($pageId)) {
            return null;
        }

        if (!$page->getId()) {
            return null;
        }

        return $page;
    }

}
  • What is the exact location of Di.xml file, is it app/code/Magenik/NavLink/etc/frontend/di.xml and what should be the location of menu and file name for the menu, is it app/code/Magenik/NavLink/Plugin/Block/Topmenu.php ? – winnersingh Jan 12 '19 at 13:41
  • So What I did is now I add the di.xml file into the location - app/code/Magenik/NavLink/etc/frontend/di.xml and topmenu.php file into the location app/code/Magenik/NavLink/Plugin/Block/Topmenu.php but still it does not work :( – winnersingh Jan 14 '19 at 13:24
0

Follow below instruction for create top Home and About menu before category.

Find below file path in your theme directory and update code. If not exists same path then create same file path and add topmenu.phtml and put below code.

File path:

magento/app/design/frontend/Vendor/Theme/Magento_Theme/templates/html/topmenu.phtml

<?php $columnsLimit = $block->getColumnsLimit() ?: 0; ?>
<?php $_menu = $block->getHtml('level-top', 'submenu', $columnsLimit) ?>

<?php
    $currentUrl = $block->getCurrentUrl();
    $baseUrl = $this->getUrl();
?>

<nav class="navigation" data-action="navigation">
    <ul data-mage-init='{"menu":{"responsive":true, "expanded":true, "position":{"my":"left top","at":"left bottom"}}}'>

        <li class="level0 home <?php if($currentUrl == '/'): ?>active<?php endif; ?>"><a href="<?php echo $baseUrl; ?>" title="<?php echo __('Home') ?>" class="level-top"><span>Home</span></a></li>

        <li class="level0 <?php if($currentUrl == '/about-us'): ?>active<?php endif; ?>"><a href="<?php echo $baseUrl.'about-us'; ?>" title="<?php echo __('About') ?>" class="level-top"><span>About</span></a></li>

        <?= /* @escapeNotVerified */ $_menu ?>
        <?= /* @escapeNotVerified */ $block->getChildHtml() ?>
    </ul>
</nav>

Let me know if any query.

Hope it help!

  • no it still does not work even I clear the cache via putty – winnersingh Jan 24 '19 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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