Magento 2 - 添加静态页面到导航

时间:2017-03-20 07:39:04

标签: magento magento2

我正在Magento 2的一家商店工作。它基于Magento Luma主题。

但现在我有点卡住了 - 我如何将静态cms页面与商店类别一起添加到导航中?

3 个答案:

答案 0 :(得分:1)

请在default.xml文件中写下以下位置的代码 -

vendor/magento/module-theme/view/frontend/layout

现在用您的网页名称和网址替换静态Cms页面名称和网址。

<referenceContainer name="catalog.topnav">
               <block class="Magento\Framework\View\Element\Html\Link\Current" name="your.link">
                    <arguments>
                        <argument name="label" xsi:type="string">Link-name</argument>
                        <argument name="path" xsi:type="string">Link-url</argument>
                    </arguments>
              </block> 
</referenceContainer>

答案 1 :(得分:0)

Magento2添加cms页面链接到菜单或Magento2添加自定义链接到菜单

首先创建一个模块

文件

  • app\code\{VendorName}\{ModuleName}\etc\module.xml
  • app\code\{VendorName}\{ModuleName}\registration.php
  • app\code\{VendorName}\{ModuleName}\composer.json

第二次创建 di.xml 我们将在其中定义插件

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>

第三次创建 di.xml 我们将在其中定义插件

app\code\{VendorName}\{ModuleName}\Plugin\Topmenu.php

代码

<?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;
    }

}

更多详细信息,请访问

http://www.amitbera.com/magento2-add-a-cms-page-link-to-menu/

答案 2 :(得分:0)

想要在<header>中添加指向顶部导航的链接 添加指向CMS页面的链接,图库

在此处编辑/放置default.xml:

app/design/frontend/Vendor/theme/Magento_Theme/layout/default.xml

添加以下代码:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="catalog.topnav">
           <block class="Magento\Framework\View\Element\Html\Link\Current" name="gallery.link">
                <arguments>
                    <argument name="label" xsi:type="string">Gallery</argument>
                    <argument name="path" xsi:type="string">gallery</argument>
                </arguments>
          </block> 
       </referenceContainer>
    </body>
</page>

这会添加一个指向CMS页面,图库的链接,其中包含以下设置:

Title = Gallery
Url Key = gallery
Link = https://example.com/gallery/

添加以下样式以确保新链接正确对齐:

.navigation .nav.item {
margin: 0 10px 0 0;
display: inline-block;
position: relative;
}

Results of the code (产品设置为示例类别)