I am looking at displaying the names of the sub-categories that have been selected within the admin area of Magento on the single product page.
I have the template open, but I just need to call the relevant code, any ideas?
Try this
<?php
$onCatalog = false;
if(Mage::registry('current_product')) {
$onCatalog = true;
}
Try this (assuming you are inside the product view template, view.phtml):
<?php foreach($_product->getCategoryCollection() as $_cat): ?>
<?php echo $_cat->getName() ?><br />
<?php endofreach ?>
That should get you started and get a list of the categories the product has been assigned to.
If you would rather thave the IDs:
<?php $categoryIds = $_product->getCategoryIds() // an array ?>
You could use this.
<h2>This product is in the following categories</h2>
<ul>
<?php
$categories = $_product->getCategoryCollection();
$categories->addAttributeToSelect(array('name', 'url'));
foreach ($categories as $category){
if ($category->getName() == 'Default Category' || $category->getName() == 'Categories') {
continue;
}
?>
<li><a href="<?php echo $category->getUrl() ?>"><?php echo $category->getName() ?></a></li>
<?php } ?>
</ul>
OK, if your're in catalog>product>view.phtml or catalog>product>list.phtml
<?php foreach($_product->getCategoryCollection() as $_cat): ?>
<?php echo $_cat->getName() ?><br />
<?php endforeach ?>
Otherwise, first line should get your the product:
$_product = Mage::registry('current_product');
Would give your currently selected product.
While:
$_product = Mage::helper('catalog/product')->load(35)
Would get you product 35.