Only Display Top-Level Categories In Your Magento Theme

back to tech articles
Magento Community Edition 1.7.0.2

So you only want to show a list of your top-most categories, not all the children and their own spawn in turn; navigate to a file called Topmenu.php (app/code/core/Mage/Page/Block/Html/Topmenu.php) and look for the following conditional statement (around line 117).

1
2
3
4
5
6
7
8
9
10
11
12
if ($child->hasChildren()) {
  if (!empty($childrenWrapClass)) {
    $html .= '<div class="' . $childrenWrapClass . '">';
  }
  $html .= '<ul class="level' . $childLevel . '">';
  $html .= $this->_getHtml($child, $childrenWrapClass);
  $html .= '</ul>';

  if (!empty($childrenWrapClass)) {
    $html .= '</div>';
  }
}

And… comment it out, like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
if ($child->hasChildren()) {
  if (!empty($childrenWrapClass)) {
    $html .= '<div class="' . $childrenWrapClass . '">';
  }
  $html .= '<ul class="level' . $childLevel . '">';
  $html .= $this->_getHtml($child, $childrenWrapClass);
  $html .= '</ul>';

  if (!empty($childrenWrapClass)) {
    $html .= '</div>';
  }
}
*/

Done. No more unwanted children in your top-level nav.

Note: this is a quick and dirty hack. You should always find a better way, such as write your own navigation function.

Update: The Magento Admin Way

It is now possible to do this through the admin area of Magento.

Navigate to Admin > Configuration > Catalog (Catalog) > Category Top Navigation

Then set Maximal Depth to 1. Clear the cache(s) and you are done.

Leave a Reply

Your email address will not be published. Required fields are marked *