Magento: Get parent category, sub-category and product count

Here is a quick and complete code to load parent category from current category page and to get sub categories of parent category along with their product count.


/** 
 * If you are in any category page then load its parent category
 * Else load root category; generally root category has ID = 3  
 *
 **/
if (is_object(Mage::registry('current_category'))) {
	$parentCategory =  Mage::getModel('catalog/category')->load(Mage::registry('current_category')->getId())->getParentCategory();
} else {	
	$rootCategoryId = 3;
	$parentCategory =  Mage::getModel('catalog/category')->load($rootCategoryId);
}

//$_categories = $parentCategory->getChildren(); 

$_categories = $parentCategory->getChildrenCategories();
$productCollection = Mage::getResourceModel('catalog/product_collection');

//$layer = Mage::getSingleton('catalog/layer');
//$layer->prepareProductCollection($productCollection);

$productCollection->addCountToCategories($_categories);

// Displaying active categories with their product count
foreach ($_categories as $_category): 
	if($_category->getIsActive()): 
?>
		<a href="<?php echo $_category->getUrl() ?>">
			<?php echo Mage::helper('core')->htmlEscape($_category->getName()) ?>
		</a> 
		<?php echo '('.$_category->getProductCount().')'; ?>
		<br />
<?php		
	endif; 
endforeach; 
?>

Hope it helps. Thanks.