想要magento中的2级类别名称

 $product = Mage::getModel('catalog/product')->load($item->getProductId());
 $pro = $product->getCategoryName(); //category id is fetched here
 $category = Mage::getModel('catalog/category')->load($pro);
  • Root Catalog
    • Furniture
    • Electronics
      • Computer
        • Processor
    • Apparel

So when i get processor as category name, i want to display its level 2 category name that is Electronics

$CategoryCollection=Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('level',2);

You can use this

$categories = Mage::getModel('catalog/category')
                ->getCollection()
                ->addAttributeToSelect('*')
                ->addIsActiveFilter()
                ->addAttributeToFilter('level',2)

OR

<?php

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

$_category = $this->getCurrentCategory();

// Category Name

echo $_category->getName();

// Category Level

echo $_category->getLevel();

?>

got the solution, The code below will get the current category and then keep getting the parent category until it gets the highest category (but not the root category)

$product = Mage::getModel('catalog/product')->load($item->getProductId());
                    $pro = $product->getCategoryName(); //category id is fetched here

                    $category = Mage::getModel('catalog/category')->load($pro);

                    if ($category)
                    {
                        while($category->getLevel() != 2)
                        {
                            $category = $category->getParentCategory();
                            if (!$category)
                            {
                                break;
                            }
                        }
                        if ($category)
                        {
                            echo $category->getName();
                        }
                        else
                        {
                            echo 'Cannot find parent category';
                        }
                    }

Yo can use something like this

public function get_cat_from_product($prod_id,$level){

    $categoryIds = Mage::getModel('catalog/product')->load($prod_id)->getCategoryIds();

    if(count($categoryIds) ){
        $firstCategoryId = $categoryIds[$level-1]; //level 2 -> pos 1
        $_category = Mage::getModel('catalog/category')->load($firstCategoryId);

        echo $_category->getName();
    }else{
        return null;
    }
}