Magento – How to get Category Id of current product

categorymagento-1.9

I have get the current product category id on product details page. I have used some method like :

$_product = Mage::getModel('catalog/product')->load(prodId);
$ids = $_product->getCategoryId();
$cat = Mage::getModel('catalog/product')->setId($ids);

but its not work as i want.

$products = Mage::getResourceModel('reports/product_collection')
        ->addAttributeToSelect('*')     
        ->setStoreId($storeId)
        ->addStoreFilter($storeId)
        ->addViewsCount()
        ->addCategoryFilter($cat)
        ->setPageSize($productCount); 

but its return some times this error

 Fatal error: Call to a member function getId() on a non-object in /var/www/html/app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php on line 719 

enter image description here

Best Answer

Because 1 product can stored in multi categories, so when call

$categoryIds = $_product->getCategoryIds();

it will a array.

foreach($categoryIds as $id) {
  $cat = Mage::getModel('catalog/category')->load($id);
}