2017-03-20 262 views

回答

1

請寫在default.xml中文件的代碼下面與位置

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

你的頁面名稱和URL現在取代靜態的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> 
+0

當我做這個「鏈接名稱」的新造型是怪異(這是其他鏈接的下面,旁邊不浮於鏈接。如果我添加類「level0」它看起來更好,但將類添加到參數列表不起作用)。有任何想法嗎? –

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,我們將DEFI NE插件

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/

0

checkout出來要內<header>
添加鏈接添加到頂部導航鏈接到CMS頁面,圖庫

編輯/放置默認值。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 (產品是建立成一個類的實例)