Magento-1.7 – Get Category Name and URL on Product Template

breadcrumbscatalogmagento-1.7template

I'm trying to get breadcrumbs working on product page. I need to display current category and url

template file:
catalog/product/view.phtml

I have tried the following:

$categories = $_product->getCategoryIds();
foreach($categories as $categoryId){
    $_category = Mage::getModel('catalog/category')->load($categoryId);
    echo $_category->getName();
    if($_category->getIsActive()){
        echo "yes\n";
    }

}

above code works, but it outputs all categories.

I have tried the following, but it didn't work

$current_cat = $this->getCurrentCategory();
echo "cat:\n";
print_r($current_cat);
//$category = Mage::registry('current_category');
//print_r($category->getName());
$c = Mage::getModel('catalog/layer')->getCurrentCategory();
print_r($c->getName());

can you suggest how to get current category? or is there alternative solution?

Best Answer

Try Using this

<?php $cat_id = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); ?>
<?php $category = Mage::getModel('catalog/category')->load($cat_id); ?>
<?php echo $category->getName(); ?>