在左侧显示类别,而不是在顶部导航

时间:2015-01-02 08:19:18

标签: magento nav

我尝试在我的Navigation / left.phtml中实现了一个代码

代码如下

    <!-- subcategory code -->

    <?php

    $category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
    if(!$category->getChildren()){
     //$parentcat_id = $category->getParentCategory()->getId();
     //$category = Mage::getSingleton('catalog/category')->load($parentcat_id);
     $category = $category->getParentCategory();
    }
    $categories = $category->getCollection()
            ->addAttributeToSelect(array('name', 'thumbnail'))
            ->addAttributeToFilter('is_active', 1)
            ->addIdFilter($category->getChildren());
     ?>
     <div class="block">

     <div class="block-title">
        <span><?php echo $category->getName() ?></span>

        </div>
        <div class="block-content clearfix">
    <ul class="subcategories">
        <?php foreach ($categories as $category): ?>
            <li>
                <a href="<?php echo $category->getUrl() ?>"><!--<img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $category->getThumbnail() ?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" />-->
                    <span><?php echo $category->getName() ?>
                   <?php $collection = Mage::getModel('catalog/product')->getCollection()->addCategoryFilter($category);

    echo "(".count($collection).")"; ?></span>
                    </a>
            </li>
        <?php endforeach; ?>
    </ul></div>
    </div>
    <!-- subcategory code -->

    <!-- List all categories and their second level subcategories -->
    <div class="block block-list block-categories">
        <div id="block-categories" class="block-title active">
            <strong><span>Categories </span></strong>
        </div>

    <div id="leftnav" class="block-content" style="display:block">
        <?php $helper = $this->helper('catalog/category') ?>
        <?php $categories = $this->getStoreCategories() ?>
        <?php if (count($categories) > 0): ?>
            <ul id="leftnav-tree" class="level0">
                <?php foreach($categories as $category): ?>
                    <li class="level0<?php if ($this->isCategoryActive($category)): ?> active<?php endif; ?>">

                        <?php //if ($this->isCategoryActive($category)): ?>
                            <?php $subcategories = $category->getChildren() ?>
                            <?php if (count($subcategories) > 0): ?>
                                <ul id="leftnav-tree-<?php echo $category->getId() ?>" class="level1">
                                    <?php foreach($subcategories as $subcategory): ?>
                                        <li class="level1<?php if ($this->isCategoryActive($subcategory)): ?> active<?php endif; ?>">
                                            <a href="<?php echo $helper->getCategoryUrl($subcategory) ?>"><?php echo $this->escapeHtml(trim($subcategory->getName(), '- ')) ?></a>
                                             <?php $secondLevelSubcategories = $subcategory->getChildren() ?>
                                             <?php if (count($secondLevelSubcategories ) > 0): ?>
                                <ul id="leftnav-tree-<?php echo $subcategory->getId() ?>" class="level2">
                                    <?php foreach($secondLevelSubcategories as $secondLevelSubcategory ): ?>
                                        <li class="level2<?php if ($this->isCategoryActive($secondLevelSubcategory )): ?> active<?php endif; ?>">
                                            <a href="<?php echo $helper->getCategoryUrl($secondLevelSubcategory ) ?>"><?php echo $this->escapeHtml(trim($secondLevelSubcategory ->getName(), '- ')) ?></a>
                                        </li>
                                        <?php endforeach; ?>
                                </ul>
                                <script type="text/javascript">decorateList('leftnav-tree-<?php echo $category->getId() ?>', 'recursive')</script>
                            <?php endif; ?>
                                    <?php endforeach; ?>
                                </ul>
                                <script type="text/javascript">decorateList('leftnav-tree-<?php echo $category->getId() ?>', 'recursive')</script>
                            <?php endif; ?>
                        <?php //endif; ?>
                    </li>
                <?php endforeach; ?>
            </ul>
            <script type="text/javascript">decorateList('leftnav-tree', 'recursive')</script>
        <?php endif; ?>
    </div>
    </div>

但是当我将“包含在导航菜单中”设置为“否”时,它也会从此处消失。 有什么方法可以隐藏在顶级菜单中并显示在我的侧边菜单中吗? 演示链接为http://infigic.com/ds4u/

2 个答案:

答案 0 :(得分:0)

将收集请求更改为此应该有效:

$categories = $category->getCollection()
            ->addAttributeToSelect(array('name', 'thumbnail'))
            ->addAttributeToFilter('is_active', 1)
            ->addAttributeToFilter('include_in_menu', 1) // there is the 'magic'
            ->addIdFilter($category->getChildren());

虽然,我不建议你这样做。 因为:

  1. 从视图中请求模型时,您正在打破Magento的MVC模式
  2. 你正在通过一个扩展Mage_Catalog_Block_Navigation的Block来完成你可能更容易实现的复杂事情,其中​​类别可能已经以正确的方式过滤但未按照您的意愿呈现
  3. 请看一下Magento基础模板(不仅是/ app / design / frontend / base中的那个,而是一个全新安装的Magento),你会发现从来没有在那里将在模型实例的视图中直接调用。

    对于Magento的良好实践,请参阅Alan Storm的完美博客(他也是SO成员)here,也许更具体地来说this chart展示模板(视图)应该如何与Blocks交互渲染模型。

答案 1 :(得分:0)

如果您没有local.xml文件,只需在主题的布局文件夹中创建一个文件,并使用此内容填充它:

<?xml version="1.0"?>
<layout>
    <default>
        <reference name="header">
            <action method="unsetChild"><name>top.menu</name></action>
        </reference>

        <reference name="left">
            <action method="insert"><child>top.menu</child></action>
        </reference>
    </default>
</layout>