【问题标题】:custom layout for all categories page magento所有类别页面 magento 的自定义布局
【发布时间】:2016-11-25 11:09:40
【问题描述】:

我想在产品类别页面上显示子类别图像。 1. 我用自定义代码创建了 category_listing.phtml 来显示子类别 2.创建了一个静态块

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

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

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

谢谢

【问题讨论】:

    标签: magento static block categories


    【解决方案1】:

    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>
    

    【讨论】:

    • 您好,感谢您的详细回复。它将向所有类别添加代码,请告诉我,如何使其适用于特定类别,如果我有 cat 1、2、3、4、5 并且我只想在 cat 3 和 cat 5 上显示子类别. 谢谢
    • 在这段代码 count()) : ?> 之后,你必须输入条件 getId()=='3 ' && $_category->getId()=='5'){ //这里是子类别代码 }
    【解决方案2】:

    打开文件: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>
    

    【讨论】:

    • 您好,感谢您的回复,我们可以通过静态块使用代码仅用于特定类别而不是所有类别吗??
    • 请为您的解决方案使用给定的链接。 docs.google.com/document/d/…
    • 您好,谢谢。它给了我与我的问题相同的错误。如果我在显示设置下选择 cat 1 中的静态块,它可以工作。当我在 cat 2 中调用相同的块时,它会显示类别 1 的子类别,除非我删除缓存。但是在删除缓存后,它会在类别 1 中显示 2 的子类别,简而言之,在多个类别中调用此块后,它不会显示相应猫的正确子类别,您必须每次删除缓存才能查看当前类别的正确子类别。尽管您在 view.html 中的代码运行良好,但可能我必须在那里按 cat id 过滤。
    猜你喜欢
    • 2015-05-24
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 2021-02-25
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多