How to get Current Category in Magento?

Posted by Damodar Bashyal on March 07, 2012

 

In Magento, If you are on category page than it's easy to get current category.

$_current_category=$this->getCurrentCategory();

or,

$_current_category = Mage::registry('current_category');

I needed Current Category on product info page and on the footer.phtml files. Because I had custom background set for each Category. So, Below code helped me to find the Current Category in my Magento's phtml file.

$hb = '';
$current_category = Mage::registry('current_category');
if(!is_object($current_category))
{
    $current_product = Mage::registry('current_product');
    if(is_object($current_product))
    {
        $categories = $current_product->load($current_product->getId())->getCategoryIds();
        if (is_array($categories) and count($categories))
        {
            $current_category = Mage::getModel('catalog/category')->load($categories[0]);
        }
    }
}
if(is_object($current_category))
{
    $body_image = $current_category->getData('body_image');
    if(!empty($body_image))
    {
        $hb = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'catalog/category/' . $body_image;
    }
}

Visit Here if you want to Get Magento's Current URL

purvi posted on - Tuesday 20th of May 2014 07:21:54 AM

Not working in magento 1.8

bartz posted on - Saturday 28th of June 2014 10:55:06 AM

Thanks for this useful tutorial, saved me a lot of time.

I just copied the main code, and done this:

$categoryVars = get_object_vars($current_category);
<span class="category-name"><a href="/&lt;?=$categoryVars['_data']['url_path']?&gt;">&lt;?=$categoryVars['_data']['name']?&gt;</a></span>
 
not published on website


QR Code: How to get Current Category in Magento?