所有类别页面magento的自定义布局

时间:2016-11-25 11:09:40

标签: magento static block categories

我想在产品的类别页面上显示子类别图像。 1.我创建了category_listing.phtml,其中包含自定义代码以显示子类别 2.创建一个静态块

{{block type="core/template" template="catalog/navigation/category_listing.phtml"}}

在类别显示设置中,我选择了静态块和产品'。 说我有三个类别 第1类 第2类 第3类

如果我为一个类别选择静态块只是完美但如果我在多个类别中调用静态块它会显示错误的子类别,除非我每次都删除缓存。 有没有我缺少的任何一步,请考虑我是magento的新手

由于

2 个答案:

答案 0 :(得分:2)

<强> 1。对于Magento中的自定义类别菜单导航

布局
打开app/design/frontend/base/default/layout/page.xml或您的主题等效。 将以下代码放在默认标记下:

<reference name="right">
     <block type="core/template" name="catalog.sidenav" template="page/custom.phtml" before="cart_sidebar"/>
</reference>

使用以下内容创建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 '&emsp;' . '<a href="' . $_categoryChildModel->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 '&emsp;&emsp;' . '<a href="' . $_categoryGrandchildModel->getUrl() . '"' . $bold . '>' .  $_categoryGrandchild->getName() . '(' . $_categoryGrandchildModel->getProductCount() . ')</a>';
                                        ?>
                                    </li>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </li>
        <?php endforeach ?>
</ul>

您还可以通过编码布局更新或通过管理员将此模板插入CMS页面的内容,将此模板插入任何其他页面:

{{block type="core/template" template="page/custom.phtml"}}

2.在左侧边栏中添加类别导航:

在主题中创建新文件“category_listing.phtml” -

app/design/frontend/{your_namespace}/{your_module}/template/catalog/navigation/category_listing.phtml

将以下代码放入其中:

<div class="block block-category">
   <div class="inside-box">
      <div class="block-title block-category-title">
         <h2><?php echo $this->__('Categories') ?></h2>
      </div>
      <div class="block-category-navigation">
         <ul id="category-nav">
            <?php foreach ($this->getStoreCategories() as $_category): ?>  
            <?php if($_category->name!=""):  ?>    
            <li><?php echo $this->drawItem($_category) ?></li>
            <?php endif?>  
            <?php endforeach ?>    
         </ul>
      </div>
   </div>
</div>

然后在位于主题文件夹的catalog.xml文件中调用它 -

app/design/frontend/{your_namespace}/{your_module}/layout/catalog.xml

代码:

<reference name="left">
   <-- this is new block added by us -->
   <block type="catalog/navigation" name="catalog.categorymenu" after="top.search" template="catalog/navigation/category_listing.phtml"/>

   <block type="core/template" name="left.permanent.callout" template="callouts/left_col.phtml">
   ...
   ...
   ...
</reference>

答案 1 :(得分:1)

打开文件:app / design / frontend / yourtheme / default / template / catalog / category / view.phtml 并添加以下代码:

<div class="category-grid-new">
    <?php $_columnCount = 4;?>
    <?php if ($i++%$_columnCount==0): ?>
        <ul class="sub-category">
        <?php endif; ?>
            <?php foreach ($this->getCurrentCategory()->getChildrenCategories() as $_subcat): ?>
                <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0):?> last<?php endif; ?>">
                    <a href="<?php echo $_subcat->getUrl() ?>">
                        <div class="category-img"><img src="<?php echo $_category->getImageUrl() ?>" alt="" width="100px" height="100px"/></div>
                        <div class="category-name"><?php echo Mage::helper('catalog/output')->categoryAttribute($_subcat, $_subcat->getName()) ?></div>
                    </a>
                </li>
            <?php endforeach; ?>
    <?php if ($i%$_columnCount==0): ?>
        </ul>
    <?php endif; ?> 
</div>