用Magento覆盖页面标题


Overwriting a page title with Magento

我四处寻找如何做到这一点,但什么都不起作用。

我有一个.phtml文件,想用它来覆盖当前标题(位于<title></title>标记之间)。

我在某个地方找到了$this->getLayout()->getBlock('head')->setTitle("New Title");应该做的事情,但事实并非如此。但是$this->getLayout()->getBlock('head')->getTitle();正确地返回了当前标题。

任何帮助都将是伟大的

我认为你不能在.phtml中做到这一点。我会使用local.xml文件并遵循本指南。

这允许您设置不同页面的标题,如:

<reference name="head">
    <action method="setForcedTitle"><title>Account Dashboard</title></action>
</reference>

我认为你不能在.phtml中做到这一点,这里是的简单解决方案

您已经设置了类似"向朋友发送电子邮件"的页面平铺,因此打开您的sendfriend.xml文件并设置以下代码。

 <reference name="head">
        <action method="setTitle" translate="title" module="sendfriend"><title>Email to a Friend</title></action>
    </reference>

它起作用了。。

Magento首先加载head.phtml。因此,我们无法覆盖来自另一个模板文件的标题。

我也有类似的要求。我所做的就在下面。

<title>
<?php
/*****************Customized For Title***********************/
        $url = $_SERVER['REQUEST_URI']; //Check With url
        $url = parse_url($url, PHP_URL_PATH);
        $url = explode('/',$url);
        $url_key = $url[2]; //Set The url-key           
        $cateUrl = Mage::getModel('catalog/category')->getCollection ()
        ->addAttributeToSelect ('id')
        ->addAttributeToFilter ('url_key',  $url_key) //load the  category
        ->getFirstItem (); //only 1 result ; 
        $catSel = Mage::getModel('catalog/category')->load($cateUrl->entity_id)->getMetaTitle();            
        //If Page title then Update
        if(!empty($catSel))
                 $this->getLayout()->getBlock('head')->setTitle($catSel);
        /*****************Customized For Title***********************/
        echo $this->getTitle() 
?>
</title>

在我的情况下,我需要检查url,如果url有来自类别的页面标题,它会覆盖它。

您只需在管理面板的"CMS"配置中编辑页面标题即可完成此操作。它非常简单,不需要任何编码。