How to Get Breadcrumbs in Custom Module PHTML File in Magento 1.9

magento-1.9

I want to display the category breadcrumb in my custom modules product details page phtml file as:

Home/My Module/Category/Product name

In my

\app\design\frontend\base\default\template\magenshop\recipe\view.phtml

Is this the right way of adding in phtml file?

<?php  $breadcrumbs = $this->getLayout()->getBlock('breadcrumbs');
          $breadcrumbs->addCrumb('home', array(
              'label'=>Mage::helper('catalog')->__('Home'),
              'title'=>Mage::helper('catalog')->__('Go to Home Page'),
              'link'=>Mage::getBaseUrl()
          ));
           $breadcrumbs->addCrumb('recipes', array(
              'label'=>Mage::helper('catalog')->__('Recipes'),
              'title'=>Mage::helper('catalog')->__('Go to Recipes Page'),
              'link'=> Mage::getBaseUrl().'recipes/' //  Module Url
          ));
          $breadcrumbs->addCrumb('category', array(
              'label'=>Mage::helper('catalog')->__($cat->getTitle()),
              'title'=>Mage::helper('catalog')->__($cat->getTitle()),
              'link'=> $CategoryUrl //  Category Url
          ));
    echo $this->getLayout()->getBlock('breadcrumbs')->toHtml();
  ?>

I also added in my modules layout file as:

\app\design\frontend\base\default\layout\magenshop\recipe.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>

But there is no output of any crumbs

Best Answer

Try below code:

public function indexAction(){
    $this->loadLayout();
    $breadcrumbs = $this->getLayout()->getBlock('breadcrumbs');

    $breadcrumbsBlock->addCrumb('home', array(
        'label'=>Mage::helper('catalog')->__('Home'),
        'title'=>Mage::helper('catalog')->__('Go to Home Page'),
        'link'=>Mage::getBaseUrl()
    ));

    $breadcrumbsBlock->addCrumb('my_module', array(
        'label'=>Mage::helper('catalog')->__('My Module'),
        'title'=>Mage::helper('catalog')->__('My Module'),
        'link'=> $customerLink //  Module Url
    ));

    $breadcrumbsBlock->addCrumb('category', array(
        'label'=>Mage::helper('catalog')->__($CategoryName),
        'title'=>Mage::helper('catalog')->__($CategoryName),
        'link'=> $CategoryUrl //  Category Url
    ));

    $breadcrumbsBlock->addCrumb('product', array(
        'label'=>Mage::helper('catalog')->__($ProductName),
        'title'=>Mage::helper('catalog')->__($ProductName),
        'link'=> $ProductUrl //  Product Url
    ));

    $title = array(Mage::helper('catalog')->__('My Module') , $CategoryName ,$ProductName );
    if ($headBlock = $this->getLayout()->getBlock('head')) {
        $headBlock->setTitle(join(' ', $title));
    }
}