Reputation: 645
I'm searching for a way to sort the frontend display of categories in my navigation.
This is the code for my navigation:
<div id="menu-accordion" class="accordion">
<?php
foreach ($this->getStoreCategories() as $_category): ?>
<?php $open = $this->isCategoryActive($_category) && $_category->hasChildren(); ?>
<h3 class="accordion-toggle"><a href="#"><?php print $_category->getName();?></a></h3>
<div class="accordion-content">
<ul>
<?php foreach ($_category->getChildren() as $child): ?>
<li>
<span class="ui-icon ui-icon-triangle-1-e vMenuIconFloat"></span>
<a href="<?php print $this->getCategoryUrl($child); ?>"><?php print $child->getName();?></a>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach ?>
</div>
I tried using asort()
to sort $this->getStoreCategories()
, but it resolved to an error 500, so I guess that it's not an array, but an object (which seems obvious for the objectoriented programming of magento). I tried finding a solution for object, but failed and now I'm a bit stuck.
Thanks for your help.
Upvotes: 0
Views: 1070
Reputation: 6138
The call to $this->getStoreCategories()
does not return an array. But you can build up your own array and use the key of the array as the element to sort on (assuming you want to sort by name of the category):
foreach ($this->getStoreCategories() as $_category)
{
$_categories[$_category->getName()] = $_category;
}
ksort($_categories);
Now instead of iterating over $this->getStoreCategories()
you iterate over the $_categories array. So your code would look something like:
<div id="menu-accordion" class="accordion">
<?php
$_categories = array();
foreach ($this->getStoreCategories() as $_category)
{
$_categories[$_category->getName()] = $_category;
}
ksort($_categories);
foreach ($_categories as $_category): ?>
<?php $open = $this->isCategoryActive($_category) && $_category->hasChildren(); ?>
<h3 class="accordion-toggle"><a href="#"><?php print $_category->getName();?></a></h3>
<div class="accordion-content">
<ul>
<?php foreach ($_category->getChildren() as $child): ?>
<li>
<span class="ui-icon ui-icon-triangle-1-e vMenuIconFloat"></span>
<a href="<?php print $this->getCategoryUrl($child); ?>"><?php print $child->getName();?></a>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach ?>
</div>
Upvotes: 2