Category Data – How to Retrieve in Magento 1.8 Catalog

categorymagento-1.8

I use these lines to get the data of a product in the catalog,

// Get product data.
$_helper = $this->helper('catalog/output');
$_product_id = Mage::registry('product')->getId();
$_product = Mage::getModel('catalog/product')->load($_product_id);

What about if I want to get the data of a category in the catalog?

$_helper    = $this->helper('catalog/output');
$_category  = $this->getCurrentCategory();
echo Mage::getModel('catalog/category')->load($_category->getId())->getThumbnail();

I get this error,

Fatal error: Call to a member function getId() on a non-object in
…\default\template\page\html\banner-category.phtml on line 4

I tried with these as well but the same error,

$_helper = $this->helper('catalog/output');
$_category_id = Mage::registry('category')->getId();
echo Mage::getModel('catalog/category')->load($_category_id)->getThumbnail();

Any ideas?

Best Answer

Try with this:

$_category = Mage::registry('current_category');
if ($_category) { //you are on the category page
    echo $_category->getThumbnail();
}
else {
    //you are not in a category page
}