Magento – How to add category IDs as a class on the top menu

coremenu

I'm trying to add classes such as cat-1, cat-2, …, cat-n to the <li> elements in the top menu, where the number is the category ID. I would also like to add cat-0 to the "Home" link.

At first I thought this would be fairly simple – just edit a theme phtml file or something, then I dug a little deeper and found app/code/core/Mage/Page/Block/Html/Topmenu.php which seems to be where the action is.

This seems like I file I shouldn't be messing with, and all of a sudden, I'm way out of my depth.

So what's the best way to add category ID-based class names to <li> tags in the top menu? Some kind of module that overrides a function in the aforementioned file?

Best Answer

I think the best solution will be override some method of app/code/core/Mage/Page/Block/Html/Topmenu.php.

A small tutorial how to write override extension you can find here in my answer. The only difference that you do not need override template, you need override model.