Question: 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?

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(); ?>

```
As @VladVinnikov wrote, products can belong to multiple categories so there might be no unambiguous "current category".

Except when there is
--------------------

Products can have different URLs dependent on where they are linked:

* example.com/product.html
* example.com/category1/product.hml
* example.com/category2/subcategory/product.html

If the product page was opened from a category with the category specific URL, this category is available with `Mage::registry('current_category')`.

### So to answer the question, how to get its name and url:

```
if ($category = Mage::registry('current_category')) {
    $name = $category->getName();
    $url = $category->getUrl();
} else {
    // either show nothing or fall back to load the product categories and pick one
}

```

### and how to display them:

```
<a href="<?php echo $this->quoteEscape($url) ?>"><?php echo $this->escapeHtml($name) ?></a>

```