This article shows how you can fetch all the categories related/associated with a particular product.
$product->getCategoryIds() function gives array of category ids with which the product is associated to. We can loop through this array and load each category to get the category name and url.
// Load product
$productId = 1; // YOUR PRODUCT ID
$product = Mage::getModel('catalog/product')->load($productId);
// get category ids of the product
$categoryIds = $product->getCategoryIds();
foreach($categoryIds as $categoryId) {
$category = Mage::getModel('catalog/category')->load($categoryId);
?>
Category: <a href="<?php echo $category->getCategoryUrl() ?>"><?php echo $category->getName() ?></a> <br/>
<?php
}
?>
Instead of loading category inside the loop, we can fetch categories of a product using category collection model as well. This is a better way than the above one.
// Load product
$productId = 1; // YOUR PRODUCT ID
$product = Mage::getModel('catalog/product')->load($productId);
$categoryIds = $product->getCategoryIds();
$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect("*")
->addAttributeToFilter('entity_id', $categoryIds);
foreach ($categories as $category) {
echo $category->getName() . '<br>';
}
?>
Hope this helps. Thanks.