Magento Nav Function for Dynamic Menus

I finally got tired of messing with Magento Menus.  The drawItem function really sucked when I needed to customize something.

Here is a little function that WORKS!!

function allebrumNav($parentId, $levels=1, $exclude=array(), $current_level=1){
if($current_level == 1){ $menu .= ‘<li><a href=”‘.Mage::getModel(‘catalog/category’)->load($parentId)->getUrl().'”>’.Mage::getModel(‘catalog/category’)->load($parentId)->getName().'</a><ul>’; }
$i = 1;
foreach(Mage::getModel(‘catalog/category’)->load($parentId)->getChildrenCategories() as $cat){
if(!in_array($cat->getId(), $exclude)){
$menu .= ‘<li><a href=”‘.$cat->getUrl().'”>’.$cat->getName().'</a>’;
if($current_level != $levels){ $menu .= ‘<ul>’.allebrumNav($cat->getId(), $levels, $exclude, $current_level+1).'</ul>’; }
$menu .= ‘</li>’;
if($current_level == 1){ $menu .= ‘</ul></li>’; }
return $menu;

To Use the function simple call:

The 1st Parameter is the Parent Category ID to start with. It will start there and read all the children categories.

The 2nd Parameter is how many levels down to read into.

The 3rd Parameter is and array of Category IDs to exclude from the list. Example: array(“32”, “35”, “6”)


1 Comment (+add yours?)

  1. horehtoerh
    Sep 05, 2012 @ 09:41:21


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: