2015-08-13 55 views
1

即時通訊使用magento。這是加載我的左導航欄的代碼。它將主要類別加載到一個ol中。我也想讓它加載每個li中的子類別。我已經把加載孩子類別在這裏我想知道的PHP代碼來獲取和顯示上述父類別的所有子類別。我發現了幾個修復程序,但不知道pHp很好導致了一系列錯誤。Magento:添加子菜單到左側菜單

/** 
* Category left navigation 
* 
* @see Mage_Catalog_Block_Navigation 
*/ 


if (!Mage::registry('current_category')) { 
    //no current category selected so default to showing the Products category 
    $cur_category=Mage::getModel('catalog/category')->load(51); 
    $layer = Mage::getSingleton('catalog/layer'); 
    $layer->setCurrentCategory($cur_category); 
}else{ 
    //current category selected so display the child categories for it 
    $layer = Mage::getSingleton('catalog/layer'); 
    $_category = $layer->getCurrentCategory(); 
    $currentCategoryId= $_category->getId(); 

    if($currentCategoryId == 306){ 
     //best sellers is selected so show the Product category 
     $cur_category=Mage::getModel('catalog/category')->load(51); 
     $layer = Mage::getSingleton('catalog/layer'); 
     $layer->setCurrentCategory($cur_category); 
    } 
} 

$_categories=$this->getCurrentChildCategories(); 
$_count = is_array($_categories)?count($_categories):$_categories->count(); 
if($_count): 
?> 
<div class="box layered-nav"> 
    <div class="head"> 
     <h3><?php echo $this->__('Browse By') ?> PRODUCT CATEGORY</h3> 
    </div> 
    <div class="border-creator"> 
     <div class="narrow-by"> 
      <dl id="narrow-by-list"> 
       <dd> 
        <ol> 
        <?php foreach ($_categories as $_category): ?> 
         <?php if($_category->getIsActive()): ?> 
         <li>       
          <a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="active"<?php endif ?>><?php echo $this->htmlEscape($_category->getName()) ?></a> 
         **LOAD CHILD CATEGORIES IN HERE** 
         </li> 
         <?php endif; ?> 
        <?php endforeach ?> 
        </ol> 
       </dd> 
      </dl><script type="text/javascript">decorateDataList('narrow-by-list')</script> 
     </div> 
    </div> 
</div> 
<?php 
endif; 
?> 
<!-- [ends] .browse-by // --> 

到目前爲止,我想出最好的,我知道它不是太大

<ul class="subnav"> 
<li><a></a> 
</ul> 

我感謝所有幫助極大

回答

0

檢查下面的代碼

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

現在$_subcategories = $_category->getChildrenCategories()獲取所有子類別。

+0

謝謝您的時間chanz, 我通過代碼看,我理解它的一部分,但不是全部。 這是否代表我提供的代碼頂部的if語句加載依賴頁面的不同導航欄? 我知道它問了很多,但你能告訴我究竟在這個頁面我需要插入它嗎?我曾嘗試過我最好的猜測,但它只是在隨機的CMS頁面中拉取任何分類鏈接。 – ATP

0

謝謝Chanz我用你的代碼片段,並最終想出了以下內容。花了一段時間,我有點慢,但你給了我我需要的東西。非常驚訝。

/** 
* Category left navigation 
* 
* @see Mage_Catalog_Block_Navigation 
*/ 


if (!Mage::registry('current_category')) { 
    //no current category selected so default to showing the Products category 
    $cur_category=Mage::getModel('catalog/category')->load(51); 
    $layer = Mage::getSingleton('catalog/layer'); 
    $layer->setCurrentCategory($cur_category); 
}else{ 
    //current category selected so display the child categories for it 
    $layer = Mage::getSingleton('catalog/layer'); 
    $_category = $layer->getCurrentCategory(); 
    $currentCategoryId= $_category->getId(); 

    if($currentCategoryId == 306){ 
     //best sellers is selected so show the Product category 
     $cur_category=Mage::getModel('catalog/category')->load(51); 
     $layer = Mage::getSingleton('catalog/layer'); 
     $layer->setCurrentCategory($cur_category); 
    } 
} 

$_categories=$this->getCurrentChildCategories(); 
$_count = is_array($_categories)?count($_categories):$_categories->count(); 
if($_count): 
?> 
<div class="box layered-nav"> 
    <div class="head"> 
     <h3><?php echo $this->__('') ?> PRODUCT CATEGORIES</h3> 
    </div> 
    <div class="border-creator"> 
     <div class="narrow-by"> 
      <dl id="narrow-by-list"> 
       <dd> 
        <ol> 
        <?php foreach ($_categories as $_category): ?> 
         <?php if($_category->getIsActive()): ?> 
         <li>       
          <a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="active"<?php endif ?>><?php echo $this->htmlEscape($_category->getName()) ?></a> 
          <?php $_subcategories = $_category->getChildrenCategories() ?> 
          <?php if (count($_subcategories) > 0): ?> 
           <ul> 
            <?php foreach($_subcategories as $_subcategory): ?> 
            <?php if($_category->getIsActive()): ?> 
            <li> 
             <a href="<?php echo $this->getCategoryUrl($_subcategory) ?>"<?php if ($this->isCategoryActive($_subcategory)): ?> class="active"<?php endif ?>><?php echo $this->htmlEscape($_subcategory->getName()) ?></a> 
             </a> 
            </li> 
            <?php endif; ?> 
            <?php endforeach; ?> 
           </ul> 
          <?php endif; ?> 
         </li> 
         <?php endif; ?> 
        <?php endforeach; ?> 
        </ol> 
       </dd> 
      </dl><script type="text/javascript">decorateDataList('narrow-by-list')</script> 
     </div> 
    </div> 
</div> 
<?php 
endif; 
?> 
<!-- [ends] .browse-by // --> 
+0

請標記我的答案,如果有幫助 – chanz

+0

現在完成我覺得很抱歉不承認早日 – ATP

相關問題