magento的分类导航是top menu,但多数人用
但有一些客户想要在magento的sidebar一个垂直的分类菜单导航,这样当客户在进入一个分类列表时就可以看到当前分类的统计分类
在这篇文章中,我们将讨论建立一个完整的magento垂直菜单。我们将做3级深度的分类:类别,子类别和子类别。你可以再次基础上再扩展层级,但我相信对于大部分magento商店3级是绰绰有余。
加载布局layout
打开app/design/frontend/base/default/layout/page.xml或者是你自己主题下的layout文件。将下面的代码复制到default标签下面
<reference name="right"> <block type="core/template" name="catalog.sidenav" template="page/custom.phtml" before="cart_sidebar"/> </reference>
这句代码的意思是告诉magento在把这个内容加载到我们每个页面的右边布局页面
现在我们来创建magento模板文件
创建菜单模板
我们需要做的是通过所有的商店类别,得到他们的子类别(2级),子子类别(3级),并显示它们。在这个过程中,我们在list页面需要寻找一个与当前分类id相同的分类id。当我们找到它 - 我们将会做任何事情。
创建app/design/frontend/base/default/template/page/custom.phtml 并复制下面代码
<ul> <?php $obj = new Mage_Catalog_Block_Navigation(); $storeCategories = $obj->getStoreCategories(); Mage::registry('current_category') ? $currentCategoryId = Mage::registry('current_category')->getId() : $currentCategoryId=''; foreach ($storeCategories as $_category): ?> <li> <strong><?php echo $_category->getName(); ?></strong> <?php $categoryChildren = $_category->getChildren(); ?> <?php if($categoryChildren->count()) : ?> <ul> <?php foreach($categoryChildren as $_categoryChild) : ?> <?php $_categoryChildModel = Mage::getModel('catalog/category')->load($_categoryChild->getId());?> <?php $categoryGrandchildren=$_categoryChild->getChildren(); ?> <li> <?php $currentCategoryId===$_categoryChild->getId() ? $bold="style=\"font-weight:bold\"" : $bold=''; echo ' ' . 'getUrl() . '"' . $bold . '>' . $_categoryChild->getName() . '(' . $_categoryChildModel->getProductCollection()->count() . ')</a>'; ?> </li> <?php if($categoryGrandchildren->count()) : ?> <?php foreach($categoryGrandchildren as $_categoryGrandchild) : ?> <?php $_categoryGrandchildModel = Mage::getModel('catalog/category')->load($_categoryGrandchild->getId());?> <li> <?php $currentCategoryId===$_categoryChild->getId() ? $bold="style=\"font-weight:bold\"" : $bold=''; echo '  ' . 'getUrl() . '"' . $bold . '>' . $_categoryGrandchild->getName() . '(' . $_categoryGrandchildModel->getProductCount() . ')</a>'; ?> </li> <?php endforeach; ?> <?php endif; ?> <?php endforeach; ?> </ul> <?php endif; ?> </li> <?php endforeach ?> </ul>
显示右边导航
你应该能够看到你的页面的右边栏导航。这不正是最漂亮的菜单导航,但我相信你就可以改变风格,以满足您的需求。
你好可以插入这个模板文件到其他页面。例如在magento后台cms page中插入导航菜单
{{block type="core/template" template="page/custom.phtml"}}