2011-08-05 77 views
3

所以我想這個代碼顯示在app /前端/預設/默認/目錄/類我view.phtml頁上的問題兒童和他們的孩子/顯示所有類別的兒童和他們的孩子在Magento

所以當你看到的類別頁面,你看到所有的子類別的所有孩子

這是我得到的,它是顯示子類別,但不是他們的孩子。

<?php 
    $_category = $this->getCurrentCategory(); 
    $collection = Mage::getModel('catalog/category')->getCategories($_category-  >entity_id); 
    $helper  = Mage::helper('catalog/category'); 
    ?> 

    <ul> 
    <?foreach ($collection as $cat):?> 
      <?php if($_category->getIsActive()):?> 
      <?php 
       $cur_category = Mage::getModel('catalog/category')->load($cat->getId()); 
       $_img = $cur_category->getImageUrl(); 
      ?> 
      <li> 
          <a href="<?php echo $helper->getCategoryUrl($cat);?>"> 
           <img src="<?php echo $_img?>" title="$cat->getName()"/> 
           <cite><?php echo $cat->getName();?></cite> 
          </a> 
        </li> 
      <?php endif?> 

<?php endforeach;?> 

+1

管理中的類別是否設置爲「錨點」?這通常會使他們自動包括他們的大孩子。 – clockworkgeek

+0

它們被設置爲「否」。 –

回答

7

請嘗試使用以下代碼。 主要想法是獲取主要級別類別。爲每個類別獲得兒童類別。

<?php 
     /* Get the categories that are active for the store */ 
     $_main_categories=$this->getStoreCategories(); 

     /* Get the current category the user is in */ 
     $_current_category=$this->getCurrentCategory(); 

     /* Get the current category path */ 
     $_categorypath = $this->getCurrentCategoryPath(); 
?> 

    <?php if ($_main_categories): ?> 
     <div class="box normal-nav"> 
      <div class="box-top"> 
      </div> 
      <div class="box-content"> 
        <ul> 
         <?php 
          /* This bit cycles through the categories - setting the next one to current */ 
          foreach ($_main_categories as $_main_category): 
           if($_main_category->getIsActive()): 
            $cur_category=Mage::getModel('catalog/category')->load($_main_category->getId()); 
            $layer = Mage::getSingleton('catalog/layer'); 
            $layer->setCurrentCategory($cur_category); 
         ?> 

            <li><a href="<?php echo $this->getCurrentCategory()->getUrl()?>"><?php echo $this->getCurrentCategory()->getName();?></a> 

             <?php $_maincategorylisting=$this->getCurrentCategory()?> 

             <?php $_categories=$this->getCurrentChildCategories()?> 

             <?php if($_categories->count()): ?> 
              <ul class="subcategory"> 
               <? foreach ($_categories as $_category):?> 
                <? if($_category->getIsActive()): 
                  $cur_subcategory=Mage::getModel('catalog/category')->load($_category->getId()); 
                  $layer = Mage::getSingleton('catalog/layer'); 
                  $layer->setCurrentCategory($cur_subcategory); 
                ?> 

                  <li><a href="<?php echo $this->getCategoryUrl($_category)?>"> <?php echo $_category->getName()?></a></li> 
                <? endif;?> 

               <?endforeach?> 

              </ul> 
              <?php $layer->setCurrentCategory($_current_category); ?> 

             <? endif; ?> 
            </li> 

          <?php endif; ?> 

         <?php endforeach; ?> 
        </ul> 
       </div> 
       <div class="box-bottom"> 

       </div> 
     </div> 
    <?php endif; ?> 

後來補充:

如果申請修復您的代碼看起來如下:

<?php 
     $_category = $this->getCurrentCategory(); 
     $collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id); 
     $helper  = Mage::helper('catalog/category'); 
    ?> 
    <ul> 
    <?php foreach ($collection as $cat):?> 
      <li> 
       <a href="<?php echo $helper->getCategoryUrl($cat);?>"> 
        <cite><?php echo $cat->getName();?></cite> 
       </a> 
       <?php $childLevel2Category = Mage::getModel('catalog/category')->getCategories($cat->entity_id); ?> 
       <ul> 
        <?php foreach ($childLevel2Category as $catLevel2) { ?> 
         <li> 
          <a href="<?php echo $helper->getCategoryUrl($catLevel2);?>"> 
           <cite><?php echo $catLevel2->getName();?></cite> 
          </a> 
         </li> 
        <?php } ?> 
       </ul> 
      </li> 
<?php endforeach;?> 
    </ul> 

如果您需要更多級別(更多的子DIR)使用遞歸函數重寫這個結構。

+0

此代碼無效。分類沒有顯示,也沒有他們的潛艇。 –

+0

請嘗試底部提供的新解決方案。 –

+0

這工作。謝謝! –

3

嘗試改變「是錨」設置這些父類別爲「是」。這應該允許他們在沒有任何自定義編碼的情況下顯示其子類別的產品。

+0

這沒有與我使用的這段代碼做到這一點。 –

0

我知道這是一個古老的問題,但我在搜索過程中碰到它,試圖做同樣的事情。

爲了未來訪問者的利益,上面的答案之一談到了將父類別的Is Anchor更改爲Yes,以便向孩子展示。 OP說它不適合他。

重要的是,這樣做後,你需要重新索引你的類別/產品關聯(系統 - >索引管理)。如果您有很多產品,這可能需要一些時間。

但是這樣做後......瞧!列出後裔產品。

而且在模板/目錄/類別將這個代碼http://www.creare.co.uk/magento-subcategories-category-pages/list.phtml了顯示子貓科動物的護理:

<?php 
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory(); 
$categories = $category->getCollection() 
     ->addAttributeToSelect(array('name', 'thumbnail')) 
     ->addAttributeToFilter('is_active', 1) 
     ->addIdFilter($category->getChildren()) 
?> 
<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() ?></span></a> 
     </li> 
    <?php endforeach; ?> 
</ul> 

希望這可以幫助別人。

+1

這有效,但是在控制器/幫助類中的代碼版本會更好。 – juz

相關問題