简体   繁体   English

如何使用Magento按字母顺序显示特定类别的所有子类别

[英]How to show all subcategories of a particular category in alphabetical order with Magento

I want to show all subcategories of a particular category in an alphabetical order just like I added in admin. 我想按字母顺序显示特定类别的所有子类别,就像我在admin中添加的一样。 I am showing my all subcategories in this page app/design/frontend/default/mytheme/template/catalog/product/product-listing.phtml 我将在此页面中显示我的所有子类别app/design/frontend/default/mytheme/template/catalog/product/product-listing.phtml

The code is 该代码是

<?php
$categories = Mage::getSingleton('catalog/layer')->getCurrentCategory()->getChildren();
$catArray = explode(',', $categories);
?>
<div class="categories_list">
<?php
foreach($catArray as $child){

$parentCategoryId = $child; 
$categoriesgot = Mage::getModel('catalog/category')->load($parentCategoryId)->getChildren();
$catArraygot = explode(',', $categoriesgot);
$categoriesCount = count($catArraygot);

//echo $categoriesCount;

    $_child = Mage::getModel( 'catalog/category' )->load($child );
    $products_count = Mage::getModel('catalog/category')->load($child)->getProductCount();
//############################################################################################################

//$parentCategoryId = $child;   
//$categoriesgot = Mage::getModel('catalog/category')->load($parentCategoryId)->getChildren();
$category_name = Mage::registry('current_category')->getName(); 


if($categoriesCount >= 1 AND $products_count < 1)
{
    $value = $categoriesCount." Categories";
}
else if($categoriesCount == 1 AND $products_count > 0)
{
    $value = $products_count." Products";
}
else if ($categoriesCount >= 1 AND $products_count > 1)
{
    $value = $categoriesCount." Categories";

}if($categoriesCount == 1 AND $products_count == 0)
{
    $value = $products_count." Products";
}

//echo $products_count;
    ?>


      <div class="listing">
      <a href="<?php echo $_child->getUrl() ?>">
            <img src="<?php echo $_child->getImageUrl();?>" alt="<?php echo $_child->getName()?>"  width="135" height="135"/>

               <h3 class="product-name"><?php echo $_child->getName() ?></h3></a>
                        <?php if($category_name != "Brands"){?>     <p><?php echo $value;?></p><?php }?>
  </div>
<?php 
}
?>
</div>

I don't know in which order subcategories are showing because sub-categories are all mixed up. 我不知道子类别显示的顺序,因为子类别混合在一起。 If anyone knows this,please help me out. 如果有人知道,请帮助我。 Thanks! 谢谢!

Here is how you can get the list of subcategories in an alphabetical order or in the order entered in the backend, provided you have the parent category. 如果您具有父类别,则可以按照字母顺序或在后端中输入的顺序获取子类别列表。

$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$children = Mage::getModel('catalog/category')->getCollection()
     ->addAttributeToSelect('*')
     ->addAttributeToFilter('parent_id', $category->getId()) //get only direct children of main category
     ->addAttributeToFilter('is_active', 1) //in case you want only the active categories
     ->addAttributeToSort('name') //sort by name
     //->addAttributeToSort('position') //in case you want to sort by position
;

foreach ($children as $child) {
    //do something with $child
}

This way you avoid calling load in a loop that can cause performance issues. 这样,您可以避免在可能导致性能问题的循环中调用load

i think this will work for you 我认为这对你有用

<?php
$cats =Mage::getModel('catalog/category')->getCategories(10);
$catIds = explode(',',$cats);

$categories = array();
foreach($catIds as $catId) {
       $category = Mage::getModel('catalog/category')->load($catId); 
       $categories[$category->getName()] = $category->getUrl();
}

ksort($categories, SORT_STRING);
?>

<ul>
<?php foreach($categories as $name => $url): ?>
    <li>
    <a href="<?php echo $url; ?>"><?php echo $name; ?></a>
    </li>
<?php endforeach; ?>
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Magento按字母顺序输出类别和子类别 - Magento output category and subcategories in alphabetical order 通过Magento中的单个MySQL查询删除特定类别及其所有子类别的所有产品 - Delete all products of a particular category & all it's subcategories through single MySQL Query in Magento 对于所有子类别,magento都会显示其父类别的描述 - for all subcategories magento shows the description of their parent category 如何在magento中获取当前类别及其子类别? - How to get current category and its subcategories in magento? 如何显示属于一个类别的子类别? - How to show the subcategories which belong to a category? 如何显示所选主要类别的子类别? - How to show subcategories of a selected main category? 如何将所有子类别复制到另一个类别 - How to copy all subcategories to another category 如何在Magento中显示某个类别的产品(不包含子类别) - How to display Products from a certain Category in Magento (without subcategories) OpenCart 2:默认情况下显示类别模块中的所有子类别(php) - OpenCart 2: Show all subcategories in category module by default (php) 在Wordpress中显示特定父类别的子类别下的所有帖子标题 - Show all post titles under the subcategories of a specific parent category in Wordpress
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM