当前位置: 技术文章>> 如何在Magento 2中添加动态链接到页脚

文章标题:如何在Magento 2中添加动态链接到页脚
  • 文章分类: Magento
  • 12530 阅读
系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》

本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。


添加动态链接的步骤?到Magento 2中的页脚:

步骤1 :首先,您需要在前端区域中创建default.xml布局文件。默认的 xml 文件将调用网站的每个页面。

app/code/vendor/Extension/view/frontend/layout/default.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="footer_links">
            <block class="Vendor\Extension\Block\Link" name="footer_link"/>
        </referenceBlock>
    </body>
</page>

步骤2 :现在,您需要创建一个名为Link的块文件.php或者您可以给出默认文件下块标签中描述的任何名称.xml该文件。

app/code/Vendor/Extension/Block/Link.php

<?php
namespace Vendor\Extension\Block;
use Magento\Framework\App\DefaultPathInterface;
use Magento\Framework\View\Element\Template\Context;
class Link extends \Magento\Framework\View\Element\Html\Link\Current
{
public function __construct(
         Context $context,
         DefaultPathInterface $defaultPath,
         array $data = [])
   {
         parent::__construct($context, $defaultPath, $data);
     }
     public function toHtml()
     {
         return parent::toHtml();
   }
     public function getPath()
     {
       return $this->getData('path');
     }
     public function getLabel()
     {
         return __('Footer Link');
     }
}

这是您可以向Magento 2商店网站添加页脚链接的方法。

结论:

我希望上述解决方案将帮助您在Magento 2中添加指向页脚的动态链接。


推荐文章