Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用Magento按字母顺序显示特定类别的所有子类别_Php_Magento_Magento 1.8 - Fatal编程技术网

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

Php 如何使用Magento按字母顺序显示特定类别的所有子类别,php,magento,magento-1.8,Php,Magento,Magento 1.8,我想以字母顺序显示特定类别的所有子类别,就像我在admin中添加的那样。 我在此页面app/design/frontend/default/mytheme/template/catalog/product/product-listing.phtml中显示我的所有子类别 代码是 <?php $categories = Mage::getSingleton('catalog/layer')->getCurrentCategory()->getChildren(); $catArra

我想以字母顺序显示特定类别的所有子类别,就像我在admin中添加的那样。 我在此页面app/design/frontend/default/mytheme/template/catalog/product/product-listing.phtml中显示我的所有子类别

代码是

<?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>
我不知道子类别的显示顺序,因为子类别都是混合的。 如果有人知道这一点,请帮助我。
谢谢

如果您有父类别,您可以按照字母顺序或在后端输入的顺序获取子类别列表

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

这样可以避免在循环中调用load,从而导致性能问题。

我认为这对您很有用

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

请我把我的代码贴在上面。你只要告诉我我将在哪里添加此代码。请在我上面的代码中使用你的代码并贴出更新的代码code@PrinceKumar. 您的代码有点长,缺少缩进。我不知道它到底去哪儿了。可能在最高层。请尝试查看它适合的位置,或者使您的代码看起来更漂亮,以便我可以轻松阅读。它显示对非对象上的成员函数getId的错误调用请在我上面的代码中使用此代码,并请在此处发布更新的代码。只需将getCategories10中的10替换为您的类别id的id