Magento 按名称对类别集合排序
我在菜单中有自定义链接,在下拉列表中显示类别。 为此,我在Magento 按名称对类别集合排序,magento,magento-1.7,Magento,Magento 1.7,我在菜单中有自定义链接,在下拉列表中显示类别。 为此,我在catalog/navigation->main menu.phtml (自定义文件) 现在,我想在按名称排序后显示类别。请帮助我如何按名称对类别集合进行排序。我已经在管理中设置了类别的顺序。但在前端,它显示未排序。 代码是:- <?php $defaultcategory= Mage::app()->getStore()->getRootCategoryId();?> <?php $mainchildr
catalog/navigation->main menu.phtml
(自定义文件)
现在,我想在按名称排序后显示类别。请帮助我如何按名称对类别集合进行排序。我已经在管理中设置了类别的顺序。但在前端,它显示未排序。代码是:-
<?php $defaultcategory= Mage::app()->getStore()->getRootCategoryId();?>
<?php $mainchildren = Mage::getModel('catalog/category')->getCategories($defaultcategory);?>
<ul>
<?php foreach ($mainchildren as $subcategory) : ?> <?php // 2 level ?>
<?php if($subcategory->getIsActive()):?>
<li id="show_subcat" class="<?php if($i==1): echo 'first'; endif; ?>" >
<?php $childid=$subcategory->getId();?>
<?php $subchild = Mage::getModel('catalog/category')->getCategories($childid);?>
<?php foreach ($subchild as $subchildcategory) : ?>
<?php $path=$subchildcategory->getRequestPath()?>
<?php break;?>
<?php endforeach ?>
<a href="<?php echo $this->getUrl().$path; ?>">
<?php echo $name= $subcategory->getName().$subcategory->getEnable() ?>
</a>
</li>
<?php endif;?>
<?php endforeach; ?>
</ul>
您可以尝试:
children = Mage::getModel('catalog/category')->getCategories($defaultcategory)
->addAttributeToSort('name', 'ASC');
或:
使用此代码获取集合并按照此链接获取更多详细信息如何将“Mage::getModel('catalog/category')->getCategories($defaultcategory)”替换为“$defaultcategory->getChildrenCategories()”?我认为它显示了错误,因为代码未写入topmenu.phtml中。因此它无法访问方法有些更改是:-$collection=$categories->getStoreCategories(true,true,false);。它将获取所有类别,因此根据需要放置条件。
children = Mage::getModel('catalog/category')->getCategories($defaultcategory)
->setOrder('name','ASC);
$categories = Mage::helper('catalog/category');
$collection = $categories->getStoreCategories(false,true,false);
foreach($collection as $_category)
{
//Do something
echo $_category->getName();
}