Tuesday, November 27, 2012

How to add a breadcrumb to any Magento page.


>> Here's a easy solution to add breadcrumb to any page. In core block page find _prepareLayout() method.

    <?php
        public function _prepareLayout()
        {
            $breadcrumbs = $this->getLayout()->getBlock('breadcrumbs');
            $breadcrumbs->addCrumb('home', array('label'=>Mage::helper('vendor')->__('GEAR WAREHOUSE'),
                                            'title'=>Mage::helper('vendor')->__('Home Page'), 'link'=>Mage::getBaseUrl()));
            $breadcrumbs->addCrumb('my_account', array('label'=>Mage::helper('vendor')->__('My Account'),
                                            'title'=>Mage::helper('vendor')->__('My Account'), 'link'=>Mage::getBaseUrl().'customer/account'));
                                           
            $breadcrumbs->addCrumb('my_classified', array('label'=>Mage::helper('vendor')->__('Classified'),
                                            'title'=>Mage::helper('vendor')->__('My Classified'), 'link'=>Mage::getBaseUrl().'vendor'));
                                           
            $breadcrumbs->addCrumb('an_alias', array('label'=>'Edit',
                                            'title'=>'Edit Classified', 'link'=>''));
        }
    ?>
   
    By using addCrumb() method you can set your breadcrumb to any page. In your phtml page you've to write the following code :
   
    <?php echo $this->getLayout()->getBlock('breadcrumbs')->toHtml(); ?>
   
    By default this code is written in layout page. (e.g. 1column.phtml)

>> I'll show you the another way by xml:
        <reference name="root">
            <action method="unsetChild"><alias>breadcrumbs</alias></action>
            <block type="page/html_breadcrumbs" name="breadcrumbs" as="breadcrumbs">
                <action method="addCrumb">
                    <crumbName>Home</crumbName>
                    <crumbInfo><label>Home</label><title>Home</title><link>/</link></crumbInfo>
                </action>
                <action method="addCrumb">
                    <crumbName>Parent Page</crumbName>
                    <crumbInfo><label>Parent Page</label><title>Parent Page</title><link>/Parent Page link</link></crumbInfo>
                </action>
                <action method="addCrumb">
                    <crumbName>Current Page</crumbName>
                    <crumbInfo><label>Current Page</label><title>Current Page</title></crumbInfo>
                </action>
            </block>
        </reference>



  
  You can use it in your custom module xml, admin category custom layout update section and also CMS page layout update xml section.