How to Get Sub Category List of a Current Category as Select Option

categorycategory-tree

Currently in a Category view page, its sub categories are displayed in a list format. I would like to change the list to select option so when there are too many sub categories users does not have to scroll all the way to the bottom to view the content.

Current Code:

<ul>
<?php foreach ($this->getCurrentCategory()->getChildrenCategories() as $_subcat): ?>
    <li><a href="<?php echo $_subcat->getUrl() ?>"><?php echo Mage::helper('catalog/output')->categoryAttribute($_subcat, $_subcat->getName(), 'name') ?></a></li>
<?php endforeach ?>
</ul>

How can I accomplish this so the sub-categories is displayed in a select option?

Best Answer

Just replace that code with...

<?php if($this->getCurrentCategory()->getChildrenCount() > 0) { ?>
    <select onchange="document.location = this.options[this.selectedIndex].getAttribute('rel')">
    <?php foreach ($this->getCurrentCategory()->getChildrenCategories() as $_subcat): ?>
        <option rel="<?php echo $_subcat->getUrl(); ?>"><?php echo Mage::helper('catalog/output')->categoryAttribute($_subcat, $_subcat->getName(), 'name'); ?></option>
    <?php endforeach ?>
    </select>
<?php } ?>

This will create a drop-down select box of the exact same categories that would have been displayed with the existing code, it will also navigate the user to the category when they click on a selection.

EDIT: This is now TESTED AND WORKING code.