【问题标题】:Magento add canonical link to head section via xmlMagento 通过 xml 将规范链接添加到 head 部分
【发布时间】:2014-03-24 13:28:02
【问题描述】:

我正在尝试通过我的 xml 文件将我的链接添加到 head 部分,但没有任何反应(magento 1.7.0.2)

<action method="addLinkRel"><rel>canonical</rel><href>http://fonts.googleapis.com/css?family=Dancing+Script</href></action>

请说出正确的方法。

【问题讨论】:

    标签: xml magento layout head


    【解决方案1】:

    可能是这样,显示完整的布局 XML 结构:

    <?xml version="1.0"?>
    <layout>
    
        <default>
            <reference name="head">
                <block type="core/text" name="any.name.here">
                    <action method="setText"><text><![CDATA[<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Dancing+Script">]]></text></action>
                </block>
            </reference>
        </default>
    </layout>
    

    setText 让您几乎可以做任何事情,并且对于任何第三方 js 或 css 来说都是一个安全的选择。

    【讨论】:

    • 通过模板找到了另一种方式,也许有人也需要: $headBlock = $this->getLayout()->getBlock('head'); $headBlock->addLinkRel('stylesheet', 'fonts.googleapis.com/css?family=Dancing+Script');
    • 它对您有用,您能否也将此标记为正确答案? :)
    • 没有必要使用 CDATA。有关 Magento 执行此操作的方式,请参阅下面的答案。
    • 嗯,应该不需要它——我知道。但是由于 Magento 版本不匹配或其他不确定的原因 - 您的解决方案并不总是有效。确实如此,并且 setText 是一个有效的 Magento 函数是有原因的——而不是一些丑陋的 hack。
    【解决方案2】:

    &lt;rel&gt;canonical&lt;/rel&gt; 用于向头部添加规范链接。

    要添加样式表,请使用:

    <reference name="head">
        <action method="addLinkRel">
            <rel>stylesheet</rel>
            <href>http://fonts.googleapis.com/css?family=Dancing+Script</href>
        </action>
    </reference>
    

    【讨论】:

    • 它对我有用,我正在寻找添加规范关系,我只是在 &lt;rel&gt; 标签中更改它,现在工作正常,
    【解决方案3】:

    你用过&lt;reference&gt;吗?

    <reference name="head">
        <action method="addLinkRel">
            <rel>canonical</rel>
            <href>http://fonts.googleapis.com/css?family=Dancing+Script</href>
        </action>
    </reference>
    

    也可以查看link

    【讨论】:

    • 当然:css/ring_graving.css样式表>canonicalfonts.googleapis.com/css?family=Dancing+Script</href></…>
    • 相同的代码对我有用。我已经在我的 xml 文件的 &lt;reference name="head"&gt; 标记中添加了我的 &lt;action&gt;,现在它可以工作了。我在&lt;rel&gt; 之后缺少&lt;href&gt; 标记以提供链接。
    【解决方案4】:

    在我的代码中,我通常将它作为一个块插入。见下面 google_font_material_icons 之一:

    <layout version="0.1.0">
        <default translate="label" module="page">
            <block type="page/html" name="root" output="toHtml" template="page/3columns.phtml">
                <block type="page/html_head" name="head" as="head">
                    <action method="addCss"><stylesheet>css/style.css</stylesheet><params>media=screen,projection"</params></action>
    
                    <block type="core/text" name="google_font_material_icons">
                        <action method="setText"><text><![CDATA[<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/icon?family=Material+Icons">]]></text></action>
                    </block>
    
                </block>
    

    ....

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2012-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多