title: Magento Load Categories in Admin order
body: <p>I'm trying to load all of the categories that are marked to be included in the navigation in Magento in the same order as they are in the Admin panel in order build a custom layered menu (we combine the output with another menu stored in the DB to other pages). </p>
  
  <p>This is what I am using in the function to generate the menu so far:</p>
  
  <pre><code>private function generateCategories() {
  
      $_root_category_id = Mage::app()-&gt;getWebsite(true)-&gt;getDefaultStore()-&gt;getRootCategoryId();
  
      $_current_children = Mage::getModel('catalog/category')
          -&gt;getCollection()
          -&gt;addAttributeToSelect('*')
          -&gt;addIsActiveFilter()
          -&gt;addLevelFilter(2)
          -&gt;addOrderField('position', 'asc');
  
      $i = 0;
  
      $html = '';
  
      foreach( $_current_children as $l0 ) {
          if ($l0-&gt;getID() != $_root_category_id &amp;&amp; $l0-&gt;getName() != '' &amp;&amp; $l0-&gt;getIncludeInMenu())  {
  
              $i++;
  
              if (Mage::helper('core/url')-&gt;getCurrentUrl() == $l0-&gt;getURL())
                  $active = ' active';
              else
                  $active = '';
  
              if ($l0-&gt;hasChildren())
                  $parent = ' parent';
              else
                  $parent = '';
  
              $html .= '&lt;li class=" level0' . $active . $parent . '"&gt;&lt;a href="' . $l0-&gt;getURL() . '" class="top"&gt;' . $l0-&gt;getName() . '&lt;/a&gt;';
  
              if ($l0-&gt;hasChildren()) {
                  $multiplier = 1;
                  $iteration = 0;
  
                  $level1 = '';
                  $level1[] = '';
  
                  foreach ( explode(',', $l0-&gt;getChildren()) as $l1 ) {
  
                      $l1 = Mage::getModel('catalog/category')-&gt;load($l1);
  
                      if ( $l1-&gt;getIncludeInMenu() ) {
  
                          if (Mage::helper('core/url')-&gt;getCurrentUrl() == $l1-&gt;getURL())
                              $active = ' active';
                          else
                              $active = '';
  
                          if ($iteration == $this-&gt;perColumn) {
                              $iteration = 0;
                              $multiplier++;
                          }
  
                          $iteration++;
  
                          $level1[] = '&lt;span class="level1' . $active . '"&gt;&lt;a href="' . $l1-&gt;getURL() . '" title="' . $l1-&gt;getName() . '"&gt;' . $l1-&gt;getName() . '&lt;/a&gt;&lt;/span&gt;';
                      }
                  }
  
                  unset($level1[0]);
  
                  $numLinks = count($level1);
                  $columns = $numLinks / $this-&gt;perColumn;
  
                  $html .= '&lt;div class="border-cover"&gt;&lt;/div&gt;&lt;div class="dropdown" style="width: ' . $this-&gt;colWidth * $multiplier . 'em;"&gt;';
  
                  $used = 0;
                  $iteration = 0;
  
                  foreach( $level1 as $link ) {
                      $used++;
                      $iteration++;
  
                      if ($used == 1)
                          $html .= '&lt;div class="col" style="float: left; width: ' . $this-&gt;colWidth . 'em;"&gt;';
  
                      $html .= $link;
  
                      if ($used == 4 || $iteration == $numLinks) {
                          $html .= '&lt;/div&gt;';
                          $used = 0;
                      }
                  }
  
                  $html .= '&lt;/div&gt;';
              }
  
              $html .= '&lt;/li&gt;';
          }
      }
  
      return $html;
  
  }
  </code></pre>
  
  <p>I was under the impression that <code>-&gt;addOrderField('position', 'asc')</code> should filter the categories into the same order as in the Admin panel, but this only works on the first level (<code>$l0</code>) categories, not the sub categories as well. </p>
  
  <p>Can anyone suggest how this could be modified for this to work?</p>