1

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?

2

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.

1
<?php $_helper = Mage::helper('catalog/category');  $_categories = $_helper->getStoreCategories();  $currentCategory = Mage::registry('current_category')  if (count($_categories) > 0): foreach($_categories as $_category): ?>
<li class="haschild">
    <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
        <?php echo $_category->getName() ?>
    </a>
    <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
    <?php $_subcategories = $_category->getChildrenCategories() ?>
    <?php if (count($_subcategories) > 0): ?>
        <ul class="">
            <?php foreach($_subcategories as $_subcategory): ?>
                <li>
                    <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
                        <?php echo $_subcategory->getName() ?>
                    </a>
                    <?php $_categorysub = Mage::getModel('catalog/category')->load($_subcategory->getId()) ?>
                    <?php $_subcategoriessub = $_categorysub->getChildrenCategories() ?>
                    <?php if (count($_subcategoriessub) > 0): ?>

                    <ul class="submenu">
                        <?php foreach($_subcategoriessub as $_subcategorysub): ?>
                        <li>
                            <a href="<?php echo $_helper->getCategoryUrl($_subcategorysub) ?>">
                            <?php echo $_subcategorysub->getName() ?>
                            </a>
                            <?php $_categorysubtosub = Mage::getModel('catalog/category')->load($_subcategorysub->getId()) ?>
                    <?php $_categorysubtosub = $_categorysubtosub->getChildrenCategories() ?>
                         <?php if (count($_categorysubtosub) > 0): ?>
                            <ul class="submenu1 <?php echo 'p'.$_subcategorysub->getId() ?>">
                                <?php foreach($_categorysubtosub as $_categorysubtosub): ?>
                                <li>
                                    <a href="<?php echo $_helper->getCategoryUrl($_categorysubtosub) ?>">
                                    <?php echo $_categorysubtosub->getName() ?>
                                    </a>
                                </li>
                                <?php endforeach; ?>
                            </ul>
                        <?php endif; ?>
                        </li>
                        <?php endforeach; ?>
                    </ul>

                    <?php endif; ?>

                </li>
            <?php endforeach; ?>
        </ul>
    <?php endif; ?>
</li>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.