How to order categories in top navigation or sidebar

Last modified by amartinez on Thu, June 24, 2010 17:08
Source|Old Revisions  

This is an old revision of the document!

Edit your template/catalog/navigation/top.phtml

To make it simple, we will sort it like a normal Array:

My app/design/frontend/default/f002/template/catalog/navigation/top.phtml:

 * Magento
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to so we can send you a copy immediately.
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to for more information.
 * @category   design_default
 * @package    Mage
 * @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (
 * @license  Academic Free License (AFL 3.0)
 * Top menu for store
 * @see Mage_Catalog_Block_Navigation
<div class="header-nav-container">
    <div class="header-nav">
        <h4 class="no-display"><?php echo $this->__('Category Navigation:') ?></h4>
        <ul id="nav">
        <li><a href="<?php echo $this->getUrl() ?>"><?php echo $this->__('Home') ?></a></li>
        <li onmouseover="toggleMenu(this,1)" onmouseout="toggleMenu(this,0)">
            <a href="<?php echo $this->getUrl('') ?>"><?php echo $this->__('Products') ?></a>
                function ordena_categorias(&$array, $subkey="id", $sort_ascending=true) {
                    if (count($array))
                        $temp_array[key($array)] = array_shift($array);
                    foreach($array as $key => $val){
                        $offset = 0;
                        $found = false;
                        foreach($temp_array as $tmp_key => $tmp_val) {
                            if(!$found and strtolower($val[$subkey]) > strtolower($tmp_val[$subkey])) {
                                $temp_array = array_merge((array)array_slice($temp_array,0,$offset), array($key => $val), array_slice($temp_array,$offset));
                                $found = true;
                            $temp_array = array_merge($temp_array, array($key => $val));
                    if ($sort_ascending)
                        $array = array_reverse($temp_array);
                        $array = $temp_array;
                $temp = Array();
                foreach ($this->getStoreCategories('name') as $_category):
                    $temp[] = $_category;
                ordena_categorias($temp, "name");
                foreach ($temp as $_category):
                    echo $this->drawItem($_category);
        <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('menu')->toHtml(); ?>
    <?php echo $this->getChildHtml('topLeftLinks') ?>

I hope it helps ;-) amartinez [at] reinizia [dot] com


Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs