2013-08-30 40 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

这可能适用于我,将会检查这个 –

相关问题