Category Navigation Listings in Magento

piaoling  2011-06-21 16:30:56

wrote an article a while about illustrating how to show categories in a tree format. The code could be altered to display categories, categories and the current category's subcategory or categories and all sub-categories. While this code works fine, I have recently stumbled on a much better, neater way to accomplish this. This method requires less database interaction and therefore should render faster on the frontend.

Listing Categories in Magento 1.4.1.1

Firstly, you need to make sure the block that you're working is of the type catalog/navigation. If you're editing catalog/navigation/left.phtml then you should be okay.

In my previous example, we used the Category model to load in category information. This can be slow and often loads in more information than we need for a simple navigation menu. This version uses the Varien_Data_Tree class.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<div id="leftnav"><br>
    <?php $helper = $this->helper('catalog/category') ?><br>
    <?php $categories = $this->getStoreCategories() ?><br>
    <?php if (count($categories) > 0): ?><br>
        <ul id="leftnav-tree" class="level0"><br>
            <?php foreach($categories as $category): ?><br>
                <li class="level0<?php if ($this->isCategoryActive($category)): ?> active<?php endif; ?>"><br>
                    <a href="<?php echo $helper->getCategoryUrl($category) ?>"><span><?php echo $this->escapeHtml($category->getName()) ?></span></a><br>
                    <?php if ($this->isCategoryActive($category)): ?><br>
                        <?php $subcategories = $category->getChildren() ?><br>
                        <?php if (count($subcategories) > 0): ?><br>
                            <ul id="leftnav-tree-<?php echo $category->getId() ?>" class="level1"><br>
                                <?php foreach($subcategories as $subcategory): ?><br>
                                    <li class="level1<?php if ($this->isCategoryActive($subcategory)): ?> active<?php endif; ?>"><br>
                                        <a href="<?php echo $helper->getCategoryUrl($subcategory) ?>"><?php echo $this->escapeHtml(trim($subcategory->getName(), '- ')) ?></a><br>
                                    </li><br>
                                <?php endforeach; ?><br>
                            </ul><br>
                            <script type="text/javascript">decorateList('leftnav-tree-<?php echo $category->getId() ?>', 'recursive')</script><br>
                        <?php endif; ?><br>
                    <?php endif; ?><br>
                </li><br>
            <?php endforeach; ?><br>
        </ul><br>
        <script type="text/javascript">decorateList('leftnav-tree', 'recursive')</script><br>
    <?php endif; ?><br>
</div>

If you copy and paste the above code into catalog/navigation/left.phtml, it should work straight away. It should list all categories and any first level subcategories of the current category. You can modify this code to display all sub-categories by removing the IF statement at line 9.

类别 :  magento(258)  |  浏览(3268)  |  评论(0)
发表评论(评论将通过邮件发给作者):

Email: