molleman
molleman

Reputation: 2946

How to get the category name from list.phtml in Magento

So i am trying to display a custom list.phtml file from within a block. thats fine i am able to display all the products with category id 6.

{{block type="catalog/product_list" category_id="6" template="catalog/product/list.phtml"}}

the above works fine. But now i want to get access to category id 6's name, how would i go about doing this from within list.phtml or even from within a different block. i just need the name of the category for the id =6 .

Upvotes: 12

Views: 49069

Answers (1)

Vinai
Vinai

Reputation: 14182

Inside the list.phtml block template you can get the category name with

<?php echo $this->getLayer()->getCurrentCategory()->getName() ?>

In this case the current category is set on the layer by the catalog/product_list block in the _getProductCollection() call.

Inside the CMS page content there is no way I know of to access the category name directly.
From a different block getting the category name might be more involved. You can try

<?php echo Mage::getSingleton('catalog/layer')->getCurrentCategory()->getName() ?>

Of course it might be the case that there is no current category might set on the layer instance, so make sure to check for that to avoid ugly errors.
Basically, if the catalog/product_list product list block's _beforeToHtml() method has been executed the current category will be set on the layer.

EDIT: All this is assuming you want to get the category name without specifying the category ID again. If you don't care about that you can always get the category name with

<?php echo Mage::getModel('catalog/category')->load($this->getCategoryId())->getName() ?>

Upvotes: 40

Related Questions