Wednesday, December 18, 2019

How to add the CMS page link to the main menu in Magento 2?


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()
  1. Create a module.
2. Create di.xml where you will define plugin.
  1. <?xml version="1.0"?>
  2. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  4. <type name="Magento\Theme\Block\Html\Topmenu">
  5. <plugin name="add_cms_menu" type="{VendorName}{ModuleName}\Plugin\Topmenu" sortOrder="1" />
  6. </type>
  7. </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
  1. <?php
  2. namespace {VendorName}\{ModuleName}\Plugin;
  3. use Magento\Framework\Data\Tree\NodeFactory;
  4. class Topmenu
  5. {
  6. protected $nodeFactory;
  7. protected $_storeManager;
  8. protected $_pageFactory;
  9. protected $_urlBuilder;
  10. public function __construct(
  11. NodeFactory $nodeFactory,
  12. \Magento\Cms\Model\PageFactory $pageFactory,
  13. \Magento\Store\Model\StoreManagerInterface $storeManager,
  14. \Magento\Framework\UrlInterface $urlBuilder
  15. ) {
  16. $this->nodeFactory = $nodeFactory;
  17. $this->_pageFactory = $pageFactory;
  18. $this->_storeManager = $storeManager;
  19. $this->_urlBuilder = $urlBuilder;
  20. }
  21. public function beforeGetHtml(
  22. \Magento\Theme\Block\Html\Topmenu $subject,
  23. $outermostClass = '',
  24. $childrenWrapClass = '',
  25. $limit = 0
  26. ) {
  27. /* Showing Cms page About us at menu */
  28. $page = $this->getCmspage('about-us');
  29. if($page == null){
  30. return;
  31. }
  32. $node = $this->nodeFactory->create(
  33. [
  34. 'data' => [
  35. 'name' => $page->getTitle(),
  36. 'id' => $page->getIdentifier(),
  37. 'url' => $this->_urlBuilder->getUrl(null, ['_direct' => $page->getIdentifier()]),
  38. 'has_active' => false,
  39. 'is_active' => false // (expression to determine if menu item is selected or not)
  40. ],
  41. 'idField' => 'id',
  42. 'tree' => $subject->getMenu()->getTree()
  43. ]
  44. );
  45. $subject->getMenu()->addChild($node);
  46. }
  47. protected function getCmspage($identifier){
  48. $page = $this->_pageFactory->create();
  49. $pageId = $page->checkIdentifier($identifier, $this->_storeManager->getStore()->getId());
  50. if (!$pageId) {
  51. return null;
  52. }
  53. $page->setStoreId($this->_storeManager->getStore()->getId());
  54. if (!$page->load($pageId)) {
  55. return null;
  56. }
  57. if (!$page->getId()) {
  58. return null;
  59. }
  60. return $page;
  61. }
  62. }
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.