2

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

3 Answers 3

11

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();?>
1
  • Glad to be of service. You could accept and upvote if you please Commented Jun 3, 2016 at 7:10
2
<?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];?>
4
  • thank you..I tried your code, but i'm always getting the value "0" from $exp[2] for all categories
    – CodeFan
    Commented 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_h
    Commented Jun 3, 2016 at 5:08
  • and if you echo <?php echo $currentCat = Mage::registry('current_category'); ?> only?
    – s_h
    Commented 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
    – CodeFan
    Commented Jun 3, 2016 at 5:22
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.