Magento Tutorials

How to Get Category ID in Magento

In Magento, each category has its own ID (often in number form) to distinguish from the rest. Normally, getting category ID is mostly checked by a developer for a specific purpose, but a store owner can check it by himself. In this post, you will know how to get category ID as a developer and as a store owner.

For developers

There are several ways to get a category ID:

Get category id from product

// Magento: Getting categroy from product
$categoryIds = $_product--->getCategoryIds();
if(count($categoryIds) ){
$firstCategoryId = $categoryIds[0];
$_category = Mage::getModel('catalog/category')->load($firstCategoryId);
echo $_category->getData('categorysecondimage');

Get category id by category name and category parent name

Get category id by category name:

$category = Mage::getResourceModel('catalog/category_collection')->addFieldToFilter('name', 'clothing');
$cat= $category->getData();
$categoryid = $cat[0][entity_id];

In case two or more parent categories have subcategories with the same name, you can get the parent first, then get the appropriate child:

$category = Mage::getResourceModel('catalog/category_collection')
->addFieldToFilter('name', 'Men')
->getFirstItem() // The parent category
->addFieldToFilter('name', 'Clothing')
->getFirstItem(); // The child category

$categoryId = $category->getId();

For non-developers

This is the easiest way to check the category ID from backend:

Access your admin panel > Catalog > Manage Categories then in the left menu click on the category whose ID you want to check, you can see it shows the category ID next to the category name, like this:

Magento get category id

See more:

How to Get API Keys in Magento 1

How to Get All Products Grammatically in Magento

How to Get Product Attribute with Name and Value in Magento

Is it helpful?


A knowledge craver who always strive to be wiser everyday.

Notify of
Inline Feedbacks
View all comments