user2064667
user2064667

Reputation: 124

get parent category of current category

I'm working on a website where my clients want me to display the parent category of current category. I know how to display the category of current product.

Mage::registry('current_category')->getName();

But I'm struggling to get the parent category of that category. Please help me regarding this. I'm a budding developer.

Upvotes: 0

Views: 2075

Answers (3)

Menno
Menno

Reputation: 641

You can simply use this short code to get the parent category:

$parentId = Mage::registry('current_category')->getParentId();
$parent = Mage::getModel('catalog/category')->load($parentId);
exit($parent->getName());

Upvotes: 0

Mahmood Rehman
Mahmood Rehman

Reputation: 4331

To get Parent Category Id you must know the current category Id, for that you need to write

<?php
$_cat = new Mage_Catalog_Block_Navigation();
$curent_cat = $_cat->getCurrentCategory();
$curent_cat_id = $curent_cat->getId();?>

Now you can get Parent Category Id. write the code given below to get the Id

<?php $parentId=Mage::getModel('catalog/category')->load($curent_cat_id)->getParentId();
echo $parentId; // $parentId will print your current category's parent Id
?>

Upvotes: 1

chanz
chanz

Reputation: 1267

Please refer this blog it contains the necessary information to achieve your requirement

Best of Luck :)

Upvotes: 0

Related Questions