How to get top parent Category from current category object?
$category->getPath()
will return the ids of all categories from the tree root to the current one separated by slash (/
). Here is an example: 1/2/56/124/543
.
The first one is the 'root of roots'. The second one is the catalog root (default category). The rest of them are simple categories.
So you can do something like this.
$path = $category->getPath();
$ids = explode('/', $path);
if (isset($ids[2])){
$topParent = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($ids[2]);
}
else{
$topParent = null;//it means you are in one catalog root.
}
Now you can get the name and url like this:
if ($topParent){
$name = $topParent->getName();
$url = $topParent->getUrl();
}
Try this
$level = $this->getCurrentCategory()->getParentCategory()->getLevel();
if($level > 1){
echo $this->getCurrentCategory()->getParentCategory()->getName();
}
else{
echo $this->escapeHtml($_category->getName());
}
Simply copy and paste this code :)
You could use getParentCategories() on the current category and then call array_pop to get the last element. Do it twice to get the second last element.