I want to add category name in product page left column top, I have created new page called 2columns-left-product.phtml to display a product purpose. Now, I want to display a category name in left column. I just used this code,


but it didn't work for me.

It will return

fatal error[ Fatal error: Call to a member function getCategoryIds() on a non-object in /home/mtccom/public_html/m/app/design/frontend/default/theme697/template/page/2columns-left-product.phtml on line 100].

Can you guide me to fix this.

I want to add a link to a category, My new code:

enter image description here

2 Answers 2


To me it sounds like your $_product variable is not properly defined.

I reckon you should try adding the following code before the code you pasted:

$_product = Mage::registry('current_product');

Then to get the category URL you can just do:

$_categoryUrl = $_category->getUrl();

Thus your final code you should like this:

if ($_product = Mage::registry('current_product')) {
$_categoryIds = $_product->getCategoryIds();
if (count($_categoryIds)) {
    $firstCategoryId = $categoryId[0];
    $_category = Mage::getModel('catalog/category')->load($firstCategoryId);
    $_categoryUrl = $_category->getUrl(); ?>
    <a href="<?php echo $_categoryUrl; ?>"><?php echo $_category->getName(); ?></a>
<?php }
 } ?>
  • thanks, it working. But i need to give link to that category name? can you help me? May 20, 2016 at 12:05
  • @Vijayakumar To get the category URL you can call $_category->getUrl() May 20, 2016 at 12:06
  • can you give me exact code for that May 20, 2016 at 12:07
  • @Vijayakumar well that is the exact code, see my updated answer May 20, 2016 at 12:09
  • I tried, <a href="<?php echo $_categoryUrl; ?>"><?php echo $_category->getName(); ?></a>. it give error [Fatal error: Call to a member function getUrl() on a non-object in /home/mtccom/public_html/m/app/design/frontend/default/theme697/template/page/2columns-left-product.phtml on line 104] May 20, 2016 at 12:19

Reason for error is product object is not getting in left side bar. Use below code in catelog xml file.

   reference name="left"
     block type="catalog/product_view" name="product_catname" template="catalog/product/catname.phtml"
you can get the product object using this method $this->getProduct() in catanme.phtml

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.