How to get url of a category in magento

How to get url of a category in magento

September 17, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to get url of a category in magento

Displaying Second Level Category URLs in Magento

The Current Problem: Incorrect Top Menu URL

You’re currently facing an issue where the main category (second level) URL is displaying incorrectly on your Magento website. This is happening despite your code correctly retrieving and processing the category collection.

Your current approach uses the default getUrl() method to generate URLs for each category. However, this can sometimes lead to inconsistencies in the root category’s URL due to how Magento handles multi-store configurations.

The Solution: Using the Correct URL Generation Method

To resolve this issue, you should use the getUrlPath() method instead of getUrl(). This method returns a clean path without any store-specific parameters or session IDs. It ensures that the URL is always generated correctly for the root category, regardless of your store configuration.

Here’s how you can modify your code:

// ...

$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->getUrlPath() . '">' . $_category->getName() . '</a>'; // Use getUrlPath() here
            $o .= '<ul>';

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

            foreach ($categoryChildren as $categoryChildId) {

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

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

            }

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

// ...

Displaying Third Level Menu on Hover

To display the third level menu when a user hovers over a second level category, you can use JavaScript or CSS to show a hidden submenu. This is beyond the scope of PHP alone and requires front-end implementation.

I recommend using Magento’s built-in accordion functionality for this purpose. You can refer to this guide on Magento’s documentation for a step-by-step walkthrough of creating a category with child categories.

Conclusion

Changing your code to use the getUrlPath() method ensures that the URLs generated are consistent and correct, regardless of your store configuration. This should fix the incorrect top menu URL issue you’re experiencing.

Remember that displaying third level menus on hover requires front-end implementation using JavaScript or CSS, which is not covered in this PHP-centric solution. I recommend exploring Magento’s built-in features for creating multi-level category structures and using their accordion functionality for your requirements.

If you need further assistance with Magento development, feel free to check out our guide on working with multiple Magento stores for a comprehensive overview of the topic.

Catalog Themes Configuration Errors

Our network