1

我想在某些将由扩展生成的站点中插入规范的元标记。所以我在扩展的 layout.xml 中插入了以下代码:

    <reference name="head">
        <action method="addLinkRel">
            <rel>canonical</rel>
            <href><url helper="core/url/getCurrentUrl"/></href>
        </action>
    </reference>

但我总是只得到“数组”而不是 url。我究竟做错了什么?

如果我能让它工作,我是否只获得www.mystore.com/productxy.html带有www.mystore.com/productxy.html?page=3. 因为我只需要第一个,没有参数。

4

2 回答 2

1

您的代码几乎是正确的。尽管您只能helper在标签正下方的<action>标签上使用布局 xml 中的属性。幸运的是,您错误地添加了额外的<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变量返回。该变量包括查询,所以不幸的是它不像你想象的那样有用。

于 2013-09-24T20:09:49.523 回答
0

我很确定你不能那样做。你得到一个,Array因为它将<url helper="core/url/getCurrentUrl"/>XML 节点解释为一个Array. 此操作将处理函数addLinkRel而不是<url />助手(从不)。

更好(也更有趣)的方法是创建一个模块,您可以在其中定义一个新的块类型来呈现<link rel='canonical' href='{$currentUrl}' />.

这是我的做法,大约需要 4 个文件:

应用程序/代码/社区/Electricjesus/Canonical/etc/config.xml

<?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> 

应用程序/代码/社区/Electricjesus/Canonical/Block/Link.php

<?php
class Electricjesus_Canonical_Block_Link extends Mage_Core_Block_Template {

}

app/design/frontend/base/default/template/canonical/link.phtml

<?php $currentUrl = Mage::helper('core/url')->getCurrentUrl(); ?>
<link rel="canonical" href="<?php echo $currentUrl ?>" />

应用程序/etc/modules/Electricjesus_Canonical.xml

<?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>

所以这基本上只是我在几分钟内完成的草稿,我对另一种问题使用了相同的解决方案(但范围相似)。所以,如果你愿意,可以试一试。

于 2013-08-22T07:57:01.860 回答