Adding a CMS page link to menu in Magento 2 is possible without an extension
- Go into a category system where you can manage cms content.
- Create a cms static block and assign that static block to the category from the display setting tab
- Set Display Mode as Static block only. Do not assign any products to that category.
Or you can add a CMS page link to menu in Magento 2 by creating a custom module
Create plugin for class Magento\Theme\Block\Html\Topmenu and use before plugin on getHtml()
- Create a module.
2. Create di.xml where you will define plugin.
- <?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>
3. Now create the plugin class where we will add a CMS page link to the menu.
and also in this way you can add a CMS page link at navigation
- <?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
- ) {
- /* 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' => 'id',
- '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;
- }
- }
Configuring, optimizing and managing a Magento 2 eCommerce store can be a daunting task. If you are having trouble creating your store in Magento 2 then you should contact Magento 2 development services. getting expert help will increase the chances of your store attracting more visitors and rankings.