我想在一些将由扩展程序生成的网站中插入规范元标记。所以我在扩展名的layout.xml中插入了以下代码:
<reference name="head">
<action method="addLinkRel">
<rel>canonical</rel>
<href><url helper="core/url/getCurrentUrl"/></href>
</action>
</reference>
但我总是得到“数组”而不是网址。我做错了什么?
如果我能让它发挥作用,我是否只使用www.mystore.com/productxy.html
得到www.mystore.com/productxy.html?page=3
或完整的网址。
因为我只需要第一个,没有参数。
答案 0 :(得分:1)
您的代码几乎是正确的。虽然您只能在helper
标记下的标记上使用布局xml中的<action>
属性。幸运的是,您错误地添加了额外的<url>
标记,因此这应该有效:
<reference name="head">
<action method="addLinkRel">
<rel>canonical</rel>
<href helper="core/url/getCurrentUrl"/>
</action>
</reference>
Mage_Core_Helper_Url::getCurrentUrl()
会从您的REQUEST_URI
变量中返回$_SERVER
。该变量包含查询,因此不幸的是它并不像你想象的那样可用。
答案 1 :(得分:0)
我很确定你做不到。您收到Array
因为它将<url helper="core/url/getCurrentUrl"/>
XML节点解释为Array
。此操作处理函数addLinkRel
而不是<url />
帮助程序(从不)。
更好(也更有趣)的方法是创建一个模块,您可以在其中定义呈现<link rel='canonical' href='{$currentUrl}' />
的新块类型。
这是我将如何做到这一点,它将需要大约4个文件:
<?xml version="1.0"?>
<config>
<modules>
<Electricjesus_Canonical>
<version>0.1.0</version>
</Electricjesus_Canonical>
</modules>
<global>
<blocks>
<canonical>
<class>Electricjesus_Canonical_Block</class>
</canonical>
</blocks>
</global>
</config>
<?php
class Electricjesus_Canonical_Block_Link extends Mage_Core_Block_Template {
}
<?php $currentUrl = Mage::helper('core/url')->getCurrentUrl(); ?>
<link rel="canonical" href="<?php echo $currentUrl ?>" />
<?xml version="1.0"?>
<config>
<modules>
<Electricjesus_Canonical>
<active>true</active>
<codePool>community</codePool>
<version>0.1.0</version>
</Electricjesus_Canonical>
</modules>
</config>
所以,现在这样做的方法是(在local.xml
中):
<reference name="head">
<block type="canonical/link" name="canonical_link" template="canonical/link.phtml" />
</reference>
所以这基本上只是我在几分钟内制作的粗略草稿,我已经使用相同的解决方案来解决另一类问题(但范围相似)。所以,如果你愿意,可以给它一个旋转。