我正在尝试使用以下代码执行两项操作 1)目前输出是根据类别ID对类别进行排序,但我希望它与后端位置结构相匹配。 2)我希望能够根据该类别的直接子节点的数量来更改html输出,因此我可以输出没有直接子节点且具有不同css样式的类别。
到目前为止我的代码是
<?php
$parentCategoryId = 486;
$cat = Mage::getModel('catalog/category')->load($parentCategoryId);
$subcats = $cat->getChildren();
?>
<?php
foreach(explode(',',$subcats) as $subCatid)
{
$_category = Mage::getModel('catalog/category')->load($subCatid);
if($_category->getIsActive()) {
echo '<ul><a href="'.$_category->getURL().'" title="View the products for the "'.$_category->getName().'" category">'.$_category->getName().'</a>';
$sub_cat = Mage::getModel('catalog/category')->load($_category->getId());
$sub_subcats = $sub_cat->getChildren();
foreach(explode(',',$sub_subcats) as $sub_subCatid)
{
$_sub_category = Mage::getModel('catalog/category')->load($sub_subCatid);
if($_sub_category->getIsActive()) {
echo '<li class="sub_cat1"><a href="'.$_sub_category->getURL().'" title="View the products for the "'.$_sub_category->getName().'" category">'.$_sub_category->getName().'</a></li>';
$sub_sub_cat = Mage::getModel('catalog/category')->load($sub_subCatid);
$sub_sub_subcats = $sub_sub_cat->getChildren();
foreach(explode(',',$sub_sub_subcats) as $sub_sub_subCatid)
{
$_sub_sub_category = Mage::getModel('catalog/category')->load($sub_sub_subCatid);
if($_sub_sub_category->getIsActive()) {
echo '<li class="sub_cat2"><a href="'.$_sub_sub_category->getURL().'" title="View the products for the "'.$_sub_sub_category->getName().'" category">'.$_sub_sub_category->getName().'</a></li>';
$sub_sub_sub_cat = Mage::getModel('catalog/category')->load($sub_sub_subCatid);
$sub_sub_sub_subcats = $sub_sub_sub_cat->getChildren();
foreach(explode(',',$sub_sub_sub_subcats) as $sub_sub_sub_subCatid)
{
$_sub_sub_sub_category = Mage::getModel('catalog/category')->load($sub_sub_sub_subCatid);
if($_sub_sub_sub_category->getIsActive()) {
echo '<li class="sub_cat3"><a href="'.$_sub_sub_sub_category->getURL().'" title="View the products for the "'.$_sub_sub_sub_category->getName().'" category">'.$_sub_sub_sub_category->getName().'</a></li>';
}
}
}
}
}
}
echo '</ul>';
}
}
?>
非常感谢您的帮助。
答案 0 :(得分:0)
试试这个,
<?php
$category = Mage::getModel('catalog/category')->load(486);
$children = Mage::getModel('catalog/category')->getCollection()->setStoreId(Mage::app()->getStore()->getId());
$children->addAttributeToSelect('*')
->addAttributeToFilter('parent_id', $category->getId())
->addAttributeToFilter('is_active', 1)//get only active categories if you want
->addAttributeToSort('position');//sort by position
foreach ($children as $sub_subCatid){
echo $sub_subCatid->getName();
$children = Mage::getModel('catalog/category')->getCollection()->setStoreId(Mage::app()->getStore()->getId());
$children->addAttributeToSelect('*')
->addAttributeToFilter('parent_id', $sub_subCatid->getId())
->addAttributeToFilter('is_active', 1)//get only active categories if you want
->addAttributeToSort('position');//sort by position
//repeat the loop foreach ($children as $sub_subCatid){ //repeat the loop }
}
?>