'How to get url of a category in magento

am trying to display all the second level categories and my code is

<?php $storeId    = Mage::app()->getStore()->getId();

//Get category model
$_category = Mage::getModel('catalog/category')->setStoreId($storeId);

$_categoryCollection = $_category->getCollection();
$_categoryCollectionIds = $_categoryCollection->getAllIds();

//Remove root category from array
unset($_categoryCollectionIds[0], $_categoryCollectionIds[1]);

?>

<div id="accordian_hover">
<?php

$o = null;
$o .= '<ul>';

foreach ($_categoryCollectionIds as $catId) {

    $_category = $_category->load($catId);

        if($_category->getLevel() == 2) {

            $catChildren = $_category->getChildren();          

                if(!empty($catChildren)) {
                    $o .= '<li> <a href="'.$_category->getUrl().'">'.$_category->getName().'</a>';
                    $o .= '<ul>';

                    $categoryChildren = explode(",", $catChildren);

                    foreach ($categoryChildren as $categoryChildId) {

                        /* @var $_childCategory Mage_Catalog_Model_Category */
                        $_childCategory = $_category = Mage::getModel('catalog/category')->setStoreId($storeId)->load($categoryChildId);

                        $o .= '<li><a href="'.$_childCategory->getUrl().'">'.$_childCategory->getName().'</a></li>';

                        // If you wish to display the total number of products for each category, uncomment this line
                        // $o .= '<span class="totalNumberOfProducts">'.$_childCategory->getProductCollection()->count().'</span>';

                    }

                    $o .= '</ul></li>';
                }   

        }
    } $o .='</ul>';


echo $o;
?>
</div>

But the top menu url is wrong all other are displaying correct but the main category url is wrong(2nd category) Please help me , and i have to display the third level menu also when the user hover on category... Live Link http://toolskaart.com/



Solution 1:[1]

Hope the below code will solve your problem.

Mage::getModel('catalog/category')->load($_category->getId())->getUrl()

It works for me.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 atw