2013-08-30 72 views
1

我想通過爲我的單獨類別提供一些帶有子級鏈接的選項卡來讓我的大型目錄具有一定的平板友好性。因此,如果用戶點擊(第一級)頭部類別,則需要顯示包含每個直接孩子的圖片,描述和網址的所有塊以及所示的所有基礎(第三級)兒童類別的列表(第二級別)分類。請問你們有沒有這麼熱心檢查我的代碼?如何在Magento中顯示當前類別的兒童子類別

<?php 
    $layer = Mage::getSingleton('catalog/layer'); 
    $_category = $layer->getCurrentCategory(); 
    $_categories = $_category->getCollection() 

->addAttributeToSelect(array('url_key','name','image','all_children','is_anchor','description')) 
    ->addAttributeToFilter('is_active', 1) 
    ->addIdFilter($_category->getChildren()) 
    ->setOrder('position', 'ASC') 
    ->joinUrlRewrite(); 
?> 

      <?php $children = explode(",", $this->getCurrentCategory()->getChildren()); ?> 

    <ul class="category-grid"> 
    <div class="category-list"> 
     <?php foreach($children as $child): ?> 
      <?php $_child = Mage::getModel('catalog/category')->load($child); ?> 
      <li class="item"> 
      <a href="<?php echo $_child->getURL() ?>" target="_self"><img title="<?php echo $this->htmlEscape($_child->getName()) ?>" src="<?php echo $this->htmlEscape($_child->getImageUrl()) ?>" alt="<?php echo $this->htmlEscape($_child->getName()) ?>" /></a> 
      <div class="subcategory-title"> 
     <a href="<?php echo $_child->getURL() ?>" title="<?php echo $this->htmlEscape($_child->getName()) ?>"><?php echo $this->htmlEscape($_child->getName()) ?></a> 
     </div> 
      <div class="description-block"> <?php echo $_child->getDescription(); ?></div> 
      <div class="children-links"><?php 
      $_helper = Mage::helper("catalog/category"); 
      $rootCat = Mage::app()->getStore()->getRootCategoryId(); 
      $current = Mage::registry('current_category'); 

       if ($child){ 
        //look for anchestor 
        $parentid = $child->getParentId(); 
        $parent = Mage::getModel("catalog/category")->load($parentid); 
        if($parentid != $rootCat) 
        { 
         //find the anchestor 
         show_cat($parent,$_helper,$rootCat); 
        }else{ 
         //is root 
      $_subcategories = $child->getChildrenCategories(); 
       echo $_child->getAll_Children(); 
       if(count($_subcategories)>0){ 
          echo '<ul>'; 
            foreach($_subcategories as $_category){ 
             echo '<li>'; 
             echo '<a href="'.$_helper->getCategoryUrl($_category).'">'.$_category->getName().'</a>'; 

              if($child->getId() == $_category->getId()){ 
               $current = Mage::registry('current_category'); 
               if ($current){ 
                //handle current 
                $_current_subcategories = $current->getChildrenCategories(); 
                 if(count($_current_subcategories)>0){ 
                  //the current cat has childrens 
                  echo '<ul>'; 
                  foreach($_current_subcategories as $_sub_category){ 
                   echo '<li>'; 
                   echo '<a href="'.$_helper->getCategoryUrl($_sub_category).'">'.$_sub_category->getName().'</a>'; 
                   echo '</li>'; 
                  } 
                  echo '</ul>'; 
                 }else{ 
                  //the current cat has no childrens 
                  $current_parent = $current->getParentId(); 
                  $current_parent = Mage::getModel("catalog/category")->load($current_parent); 
                  $_current_subcategories = $current_parent ->getChildrenCategories(); 
                  echo '<ul>'; 
                   foreach($_current_subcategories as $_sub_category){ 
                    echo '<li>'; 
                    echo '<a href="'.$_helper->getCategoryUrl($_sub_category).'">'.$_sub_category->getName().'</a>'; 
                    echo '</li>'; 
                   } 
                  echo '</ul>'; 
                  } 
                } 
               } 
             echo '</li>'; 
             } 
           echo '</ul>'; 
          } 
         } 
        } 

           ?> 
       </div> 
      </li> 
     <?php endforeach ?> 
     </div> 
    </ul> 
+0

好的,而不是上面的代碼,我剛剛添加:htmlEscape($_child->getChildren()) ?>這將獲得所有子類別children_id。任何關於如何讓他們顯示正確的名稱/網址? –

回答

1

,你可以在此通過下面的代碼,也指在底部

鏈接,你可以用這個

<?php $_helper = Mage::helper('catalog/category') ?> 
<?php $_categories = $_helper->getStoreCategories() ?> 
<?php $currentCategory = Mage::registry('current_category') ?> 
<?php if (count($_categories) > 0): ?> 
    <ul class="category"> 
     <?php foreach($_categories as $_category): ?> 
      <li> 
       <a href="<?php echo $_helper->getCategoryUrl($_category) ?>"> 
        <?php echo $_category->getName() ?> 
       </a> 
       <?php if ($currentCategory->getId() && $currentCategory->getId() == $_category->getId()): ?> 
        <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?> 
        <?php $_subcategories = $_category->getChildrenCategories() ?> 
        <?php if (count($_subcategories) > 0): ?> 
         <ul class="subcategory"> 
          <?php foreach($_subcategories as $_subcategory): ?> 
           <li> 
            <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>"> 
             <?php echo $_subcategory->getName() ?> 
            </a> 
           </li> 
          <?php endforeach; ?> 
         </ul> 
        <?php endif; ?> 
       <?php endif; ?> 
      </li> 
     <?php endforeach; ?> 
    </ul> 
<?php endif; ?> 

編輯被去

<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> 

或者你可以去扔this Detail documentation,我相信這對你真的很有幫助。

+0

感謝您的回覆,但這段代碼只是給出了所有類別,並沒有以任何方式像我在代碼中那樣過濾它們。我只需要顯示已經顯示的第三級別的孩子(查看我的代碼)第二個子級別。 –

+0

@SjorsVoorwinden,使用此http://stackoverflow.com/questions/14580970/magento-3rd-level-subcategories-menu也http://www.pauldonnelly.net/magento-display-other-products-from-the-同一類別/三級類別。肯定幫助你。如果你能接受我的回答,我會很高興。 – liyakat

+0

我剛剛在childrenlinkds div後添加了htmlEscape($_child->getChildren()) ?>。這隻顯示ID,我需要孩子的網址。你有什麼想法? –

0
// get current category 
$current_category = $layer->getCurrentCategory(); 

// get sub categories of current category 
$parent_categories = Mage::getModel('catalog/category')->getCategories($current_category->getId()); 

// go through each sub category and get their sub categories. 
foreach($parent_categories as $child_category) 
{ 
    $child_category_id = $child_category->getId(); 
    $grandchild_categories = Mage::getModel('catalog/category')->getCategories($child_category_id); 

} 
+0

這可能適用於我,將會檢查這個 –

相關問題