I'm using Magento 1.9. I would like to get the current category of products in app/design/frontend/xxxx/xxxx/template/catalog/layer/view.phtml
file.
I've already tried Mage::registry('current_category')
and Mage::getSingleton('catalog/layer')->getCurrentCategory()
for getting current category. I'm not getting any results from that. thanks in advance
Add a comment
|
3 Answers
Current Category's ID
<?php echo Mage::registry('current_category')->getId();?>
Current Category's Name
<?php echo Mage::registry('current_category')->getName();?>
Current Category's Parent's Id
<?php echo Mage::registry('current_category')->getParentId();?>
-
Glad to be of service. You could accept and upvote if you please Commented Jun 3, 2016 at 7:10
<?php
$currentCat = Mage::registry('current_category');
if (isset($currentCat) ) {
$exp = explode("/", $currentCat->getPath());
} else {
$exp = explode("/", '/1/0/10');
}
?>
then example if you want to use it in body class for css modifications design:
<body class="cat-<?php echo $exp[2];?>
-
thank you..I tried your code, but i'm always getting the value "0" from $exp[2] for all categories– CodeFanCommented Jun 3, 2016 at 4:09
-
did you try modifying the number, example [1] ? im sharing you a code Im using. in this case, exp[2] displays the subcategory 2, eg: root_catalog/computers– s_hCommented Jun 3, 2016 at 5:08
-
and if you echo <?php echo $currentCat = Mage::registry('current_category'); ?> only?– s_hCommented Jun 3, 2016 at 5:10
-
yes i tried all those. its only executing the else part and giving results like 1 or 0. Also tried echoing $currentCat = Mage::registry('current_category'); and nothing come up– CodeFanCommented Jun 3, 2016 at 5:22
you can get the category id from product name or id by loading model of product.
$catCollection = $_product->getCategoryCollection(); foreach($catCollection as $cat){ print_r($cat->getData()); //echo $cat->getName(); //echo $cat->getUrl(); }
Another Way,
$_product= Mage::getModel('catalog/product'); $catIds = $_product->getCategoryIds();
After that you can get the categories by accessing $catIds array such as $catIds[0], $catIds[1] etc.