我使用以下代码在下拉菜单中显示类别和子类别,但我希望子类别按字母顺序排序...我该怎么做?
<ul id="custom-menu">
<li><a href="http://www.mobiledistributorsupply.com"><b>Home</b></a></li>
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul style="width:940px;">
<?php foreach($_categories as $_category): ?>
<li>
<b><a class="drop" href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a></b>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<div class="dropdown_1column" style="width:235px;">
<?php foreach($_subcategories as $_subcategory): ?>
<div class="col_1">
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<?php echo $_subcategory->getName() ?>
</a>
</div>
<?php endforeach; ?>
<div class="clr" clear="all"></div>
</div>
<?php endif; ?>
</li>
<?php endforeach; ?>
<li class="normal"><a href="/products/onsale"><b>Sale</b></a></li>
<li class="normal"><a href="/customer-service"><b>Help</b></a></li>
<li class="normal"><a href="/customer/account/create/"><b>Open Account</b></a></li>
</ul>
<?php endif; ?>
答案 0 :(得分:0)
我一直在做的事情,因为我们的客户通常不会经常更改类别在管理员后端按字母顺序拖放:Catalog > Categories > Manage Categories
。
如果这不是你的风格,你总是可以从这个答案中得到一些建议,首先浏览类别,将它们放在一个新数组中,然后对数组进行键排序,最后输出:https://stackoverflow.com/a/4273912/823549
答案 1 :(得分:0)
首先备份你的topmenu.phtml然后在你的新topmenu.phtml文件中替换以下代码
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php
function array_sort($array, $on, $order=SORT_ASC){
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array;
}
?>
<?php
$layer = Mage::getSingleton('catalog/layer');
$_category = $layer->getCurrentCategory();
$currentCategoryId= $_category->getId();
?>
<div class="nav-container">
<ul id="nav">
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0){ ?>
<?php foreach($_categories as $_category){ ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<li><a href="<?php echo $_helper->getCategoryUrl($_category) ?>"><span><?php echo $_category->getName(); ?></span></a>
<?php $catList = array();?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php foreach($_subcategories as $_subCategory){ ?>
<?php $catList[] = array('name' => $_subCategory->getName(), 'url' => $_subCategory->getUrl(), 'id' => $_subCategory->getId());?>
<?php } ?>
<?php $catList = array_sort($catList, 'name', SORT_ASC);?>
<ul>
<?php if (count($catList) > 0){ ?>
<?php $subcat=0?>
<?php foreach($catList as $_subCategory){ ?>
<li><a href="<?php echo $_subCategory['url'] ?>"><span><?php echo $_subCategory['name'] ?></span></a>
<?php $subCatList = array();?>
<?php $_subSubCat = Mage::getModel('catalog/category')->load($_subCategory['id']);
$_subSubCategories = $_subSubCat->getChildrenCategories();?>
<?php foreach($_subSubCategories as $_subSubCategory){ ?>
<?php $subCatList[] = array('name' => $_subSubCategory['name'], 'url' => $_subSubCategory['url']);?>
<?php } ?>
<?php $subCatList = array_sort($subCatList, 'name', SORT_ASC);?>
<?php if (count($subCatList) > 0){ ?>
<ul>
<?php foreach($subCatList as $_subSubCat){ ?>
<li><a href="<?php echo $_subSubCat['url'] ?>"><span><?php echo $_subSubCat['name'] ?></span></a>
<?php } ?>
</li>
</ul>
<?php } ?>
</li>
<?php } ?>
<?php } ?>
</ul>
</li>
<?php } ?>
<?php } ?>
</ul>
</div>