سؤال

I searched, doesn't find a answer.

I know use layout XML to set title like below

<reference name="head">
    <action method="setTitle"><title>New Title</title></action>
</reference>

But this is set title static, I need customize the title dynamically, like "shopping with (Search Phrase)", the "Search Phrase" is dynamically generate by user input. Magento has a default dynamically search page title form, I want to know the way to change its default form.

Thanks for any help!

هل كانت مفيدة؟

المحلول

Dynamic title provided by Block/Result.php in catalogsearch module. So you have to override the Block/Result.php into your local folder. There you can change the title as per your requirement.

app/code/local/Mage/CatalogSearch/Block/Result.php

Your local file path should be like this.

class Mage_CatalogSearch_Block_Result extends Mage_Core_Block_Template
{
    /**
     * Catalog Product collection
     *
     * @var Mage_CatalogSearch_Model_Resource_Fulltext_Collection
     */
    protected $_productCollection;

    /**
     * Prepare layout
     *
     * @return Mage_CatalogSearch_Block_Result
     */
    protected function _prepareLayout()
    {
        // add Home breadcrumb
        $breadcrumbs = $this->getLayout()->getBlock('breadcrumbs');
        if ($breadcrumbs) {
            $title = $this->__("Search results for: '%s'", $this->helper('catalogsearch')->getQueryText());

            $breadcrumbs->addCrumb('home', array(
                'label' => $this->__('Home'),
                'title' => $this->__('Go to Home Page'),
                'link'  => Mage::getBaseUrl()
            ))->addCrumb('search', array(
                'label' => $title,
                'title' => $title
            ));
        }

        // modify page title
        $title = $this->__("shopping with ('%s')", $this->helper('catalogsearch')->getEscapedQueryText());
        $this->getLayout()->getBlock('head')->setTitle($title);

        return parent::_prepareLayout();
    }
}

نصائح أخرى

I am not sure but you can try below code.

<?php $this->getLayout()->createBlock('page/html_head')->setTitle($title)?>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top