4

I am Getting the sub categories of current categories but it is showing in random way or may be according to the category ID as follows:

 <?php $category_ido = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); $categories = Mage::getModel('catalog/category')->load($category_ido)->getChildren(); $catArray = explode(',', $categories); ?> <?php if($categories) { ?> <div class="category-products category-products-sub"> <ul class="products-grid"> <?php foreach($catArray as $child) { $_child = Mage::getModel( 'catalog/category' )->load( $child ); ?> <li class="item <?= $class ?> sub-cat-items"> <a href="<?php echo $_child->getURL() ?>" title="<?php echo $this-> htmlEscape($_child->getName()) ?>"><img src="<?php echo Mage::getBaseUrl('media').'catalog/category/'.$_child->getThumbnail() ?>" width="580" max-height="300px" alt="<?php echo $this->htmlEscape($_child->getName()) ?>" /></a> <h1><?php echo $this->htmlEscape($_child->getName()) ?></h1> </li> <?php } ?> </ul> </div> <?php } ?> 

So now i want to sort them Alphabetically how i can achieve that.

2
  • First you need to create a block for your template. This is a nightmare to read. For the actual question you will need to sort the content yourself using usort. It comlicates the code some more and makes it important to put all this logic in a block Commented Feb 24, 2017 at 16:20
  • if you want to sort them according alphabetical order then you can user PHP "sort" function after converting your string into array. add <?php sort(catArray); ?> before foreach loop. Commented Feb 24, 2017 at 16:31

2 Answers 2

2

use ksort

$_categories=array(); foreach($catArray as $child) { $_child = Mage::getModel( 'catalog/category' )->load( $child ); $_categories[$_child->getName()] = $_child; } ksort($_categories); 

Now it will sorted alphabetic order

 foreach($_categories as $_child) { <li class="item <?= $class ?> sub-cat-items"> <a href="<?php echo $_child->getURL() ?>" title="<?php echo $this-> htmlEscape($_child->getName()) ?>"><img src="<?php echo Mage::getBaseUrl('media').'catalog/category/'.$_child->getThumbnail() ?>" width="580" max-height="300px" alt="<?php echo $this->htmlEscape($_child->getName()) ?>" /></a> <h1><?php echo $this->htmlEscape($_child->getName()) ?></h1> </li> <?php } 
0
2

For Understand first try this

 <?php echo "<pre/>"; $categories = Mage::getResourceModel('catalog/category_collection') ->addAttributeToSort('name','ASC') ->getData(); for ($i=0; $i < count($categories); $i++) { echo $categories[$i]['name']."<br/>"; } die(); ?> 

For Output :-

<?php $_categories = Mage::getModel('catalog/category')->getCollection() ->addAttributeToSelect('name') ->addAttributeToSelect('is_active') ->addAttributeToSort('name','ASC') ->addUrlRewriteToResult(); foreach($_categories as $_category) { echo "<a href=".$_category->getUrl($_category).">".$_category->getName($_category)."</a><br/>"; //echo "<pre/>"; //echo $_category->getUrl($_category); } die; ?> 
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.