Magento:在每个前端页面的头部添加 link
Magento: adding link in head to every frontend page
我有一个模块需要在前端的每个页面上将规范 link 注入到 <head>
中。有办法吗?目前,鉴于我的模块不需要在前端有自己的页面,也不需要任何控制器,我只在 config.xml 中设置了助手。现在,我在布局中确实有一个 xml,但问题是我需要根据用户输入(在管理员中)更改规范的 link 属性,因此 XML 不适合。是的,我确实可以打开所说的前端布局 xml 文件,然后替换我需要的内容,然后将新内容写回它,但我想先检查是否有其他方法可以实现。
您可以挂钩 core_block_abstract_prepare_layout_before
事件并使用 Head 块的 addLinkRel 方法添加 link 标签。
在您的 config.xml 中,您需要像这样定义一个观察者:
<events>
<core_block_abstract_prepare_layout_before>
<observers>
<your_module>
<class>Your_Module_Model_Observer</class>
<method>addCanonicalLink</method>
</your_module>
</observers>
</core_block_abstract_prepare_layout_before>
</events>
在模型目录下创建观察者class
<?php
class Your_Module_Model_Observer {
public function addCanonicalLink(Varien_Event_Observer $observer) {
$block = $observer->getData('block');
if ($block->getNameInLayout() === 'head') {
// this will add <link rel="canonical" href="http://your-url.com">
$block->addLinkRel('canonical', 'http://your-url.com');
// If you need more attributes on the link tag use addItem instead
// This will add <link rel="canonical" href="http://your-url" attr="Your Attribute">
// $block->addItem('link_rel', 'http://your-url', 'rel="canonical" attr="Your Attribute"')
}
}
}
更新:
由于核心 head.phtml 模板文件 运行 echo $this->getChildHtml()
(渲染所有子项)可以通过添加 core/text
块作为子项来插入标签并添加它的文本字符串(就像您已经尝试过 xml 一样)。如果 addItem
不符合您的需要,这更灵活,因为您可以通过这种方式插入任何字符串。将 $block->addLinkRel
行替换为
$canonical = $block->getLayout()->createBlock('core/text')
->setText('<link rel="canonical" href="http://your-url.com">')
$block->append($canonical);
我有一个模块需要在前端的每个页面上将规范 link 注入到 <head>
中。有办法吗?目前,鉴于我的模块不需要在前端有自己的页面,也不需要任何控制器,我只在 config.xml 中设置了助手。现在,我在布局中确实有一个 xml,但问题是我需要根据用户输入(在管理员中)更改规范的 link 属性,因此 XML 不适合。是的,我确实可以打开所说的前端布局 xml 文件,然后替换我需要的内容,然后将新内容写回它,但我想先检查是否有其他方法可以实现。
您可以挂钩 core_block_abstract_prepare_layout_before
事件并使用 Head 块的 addLinkRel 方法添加 link 标签。
在您的 config.xml 中,您需要像这样定义一个观察者:
<events>
<core_block_abstract_prepare_layout_before>
<observers>
<your_module>
<class>Your_Module_Model_Observer</class>
<method>addCanonicalLink</method>
</your_module>
</observers>
</core_block_abstract_prepare_layout_before>
</events>
在模型目录下创建观察者class
<?php
class Your_Module_Model_Observer {
public function addCanonicalLink(Varien_Event_Observer $observer) {
$block = $observer->getData('block');
if ($block->getNameInLayout() === 'head') {
// this will add <link rel="canonical" href="http://your-url.com">
$block->addLinkRel('canonical', 'http://your-url.com');
// If you need more attributes on the link tag use addItem instead
// This will add <link rel="canonical" href="http://your-url" attr="Your Attribute">
// $block->addItem('link_rel', 'http://your-url', 'rel="canonical" attr="Your Attribute"')
}
}
}
更新:
由于核心 head.phtml 模板文件 运行 echo $this->getChildHtml()
(渲染所有子项)可以通过添加 core/text
块作为子项来插入标签并添加它的文本字符串(就像您已经尝试过 xml 一样)。如果 addItem
不符合您的需要,这更灵活,因为您可以通过这种方式插入任何字符串。将 $block->addLinkRel
行替换为
$canonical = $block->getLayout()->createBlock('core/text')
->setText('<link rel="canonical" href="http://your-url.com">')
$block->append($canonical);