Answers:
将以下内容放入此文件:
{theme_dir}/Magento_Theme/templates/html/copyright.phtml
<?php /* @escapeNotVerified */ echo preg_replace('/(^|\s)(\d{4})(\s|$)/m', " ".date('Y'). " ", $block->getCopyright()); ?>
<?= /* @escapeNotVerified */ str_ireplace('{{year}}', date('Y'), $block->getCopyright()) ?>
…,然后在页脚管理员中使用“ {{year}}”版权文字。这样,我可以完全控制文本以及自动更新的年份。
将以下内容放入此文件: {theme_dir}/Magento_Theme/templates/html/copyright.phtml
<small class="copyright">
<span>Copyright © You <?php echo date('Y') ?>, All Rights Reserved.</span>
</small>
然后刷新缓存。
最好的方法是在中的getCopyright方法上创建一个after插件Magento\Theme\Block\Html\Footer
。在模板中添加逻辑不是一个好习惯。
在etc/frontend/di.xml
文件的自定义模块中添加以下内容
<?xml version="1.0" encoding="UTF-8"?>
<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\Footer">
<plugin name="Vendor_Module::UpdateCopyrightWithCurrentYear" type="Vendor\Module\Plugin\Theme\Block\Html\Footer\UpdateCopyrightWithCurrentYear" />
</type>
</config>
Plugin/Theme/Block/Html/Footer/UpdateCopyrightWithCurrentYear.php
在您的模块内创建:
<?php
namespace Vendor\Module\Plugin\Theme\Block\Html\Footer;
use Magento\Theme\Block\Html\Footer;
class UpdateCopyrightWithCurrentYear
{
/**
* @param Footer $subject
* @param string $result
* @return string $result
*/
public function afterGetCopyright(Footer $subject, $result)
{
$result = preg_replace_callback(
'/(^|\s)(\d{4})(\s|$)/m',
function($matches) {
return $matches[2] != date('Y')?$matches[1] . $matches[2].' - '.date('Y') . $matches[3]:$matches[0];
},
$result);
return $result;
}
}
我借了克里希纳·ijjada的正则表达式来匹配年份。同样,这会在版权消息中添加当前年份,以便版权开始的年份也保持可见。
有必要考虑时区,这是我的答案({theme_dir}/Magento_Theme/templates/html/copyright.phtml
):
<?php
/* @var $block \Magento\Theme\Block\Html\Footer */
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
$year = ObjectManager::getInstance()->get( TimezoneInterface::class )->date()->format( 'Y' );
?>
<small class="copyright">
<span><?= /* @escapeNotVerified */ $block->escapeHtml( __( 'Copyright © %1 xxx.', $year ) ) ?></span>
</small>
这就是我要怎么做。覆盖copyright.phtml
:
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
?>
<small class="copyright">
<span><?= /* @escapeNotVerified */ str_replace ( '{{year}}', date('Y'), $block->getCopyright()) ?></span>
</small>
然后去Content->Design->Configuration
选择主题Edit->footer->copyright
添加此:
Copyright © {{year}} Magento. All rights reserved.
做完了!