Thursday, 27 September 2012

Changing head section title, meta tag or robots in magento

In magento we need to some changes specifically required for the SEO purpose. We need to change/set title, keywords and description of any page programatically.
Programmer can do in two way.
1. If you need these things to be as hard coded(not change on any condition basis) for a specific page than you can do it through Layout file
For example: if you like to set the title, robots etc for only catalog search page.
then go to app/design/frontend/default/your_theme/layout/catalogsearch.xml
Now your default section of this xml look like

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        <reference name="head">
            <action method="setTitle"><title>My Module Page</title></action>
            <action method="setKeywords"><title>My Module Page</title></action>
        </reference>
        <reference name="root">
            <!------->
        </reference>
        <reference name="right">
            <!------->
        </reference>
    </default>
</layout>
 
2. If you need this head things works on some conditions then php codding is best way:
You can work around in the of that page .php file. Suppose you want to put robot 'noindex,follow' for search pages only. Then you have to get the corresponding php file which have _prepareLayout() function. For search pages it is Results.php resides in app/code/core/Mage/catalogsearch/Block (Don't forget to override in local before making any changes.)
You can write your code in app/code/local/Mage/catalogsearch/Block/Results.php as below:
//if(---Any condition---){
 $head = $this->getLayout()->getBlock('head');
 $head->setTitle("Your Title");
 $head->setKeywords("Your Keywords");
 $head->setDescription("Your Description");
 $head->setRobots("NOINDEX,FOLLOW");
//}

No comments :

Post a Comment