我想创建一个自定义导航栏以放入我的 header.phtml 文件中。
为此,我需要所有类别的列表,我希望有一个简单的函数可以调用来获取数组?
以下获取指定类别的子类别,我认为您应该可以从这里开始工作:
$layer = Mage::getSingleton('catalog/layer');
$_category = Mage::getModel('catalog/category')->load(3); //this is cat 3, or can use:
$_category = $layer->getCurrentCategory();
$_categories = $_category->getCollection()
->addAttributeToSelect(array('url_key','name','image','all_children','is_anchor','description')) //or whatever fields you want
->addAttributeToFilter('is_active', 1)
->addIdFilter($_category->getChildren())
->setOrder('position', 'ASC')
->joinUrlRewrite();
然后是这样的:
<?php foreach ($_categories as $_category): ?>
ETC...
您也可能对。。。有兴趣:
$this->htmlEscape($_category->getImageUrl())
$this->htmlEscape($_category->getName())
$_category->getURL()
希望这有助于为您指明正确的方向。显然,load(3)
可以只是您的根类别。
如果您使用单个商店或将$_rootCatId更改为动态,请使用以下代码。
$_rootCatId = 2;
$_rootCategory = Mage::getModel('catalog/category')->load($_rootCatId);
$_catName = $_rootCategory->getName();
if($_rootCategory->hasChildren())
{
$_collection = Mage::getResourceModel('catalog/category_collection')
->addAttributeToFilter('is_active', 1)
->addAttributeToSort('position', 'asc')
->joinUrlRewrite()
->addIdFilter($_rootCategory->getChildren())
->load();
foreach($_collection AS $_sub)
{
$_subCat = Mage::getModel('catalog/category')->load($_sub->getId());
if($_subCat->hasChildren())
{
echo '<li class="leve10 nav-'. $_subCat->getId() .'">';
echo '<a id="subCatLink">';
echo '<span>'. $this->htmlEscape($_subCat->getName()) .'</span>
</a>
</li>';
echo '<ul id="subCatUl" class="no-display" style="padding:0px 13px;">';Categories();
$__collection = Mage::getResourceModel('catalog/category_collection')
->addAttributeToFilter('is_active', 1)
->addAttributeToSort('position', 'asc')
->joinUrlRewrite()
->addIdFilter($_subCat->getChildren())
->load();
foreach($__collection AS $__sub)
{
$__subCat = Mage::getModel('catalog/category')->load($__sub->getId());
echo '<li class="leve20 nav-'. $__subCat->getId() .'">
<a href="'. $this->getCategoryUrl($__subCat) .'">
<span>'. $this->htmlEscape($__subCat->getName()) .'</span>
</a>
</li>';
}
echo '</ul>';
}
else
{
echo '<li class="leve10 nav-'. $_subCat->getId() .'">
<a href="'. $this->getCategoryUrl($_subCat) .'">
<span>'. $this->htmlEscape($_subCat->getName()) .'</span>
</a>
</li>';
}
}
}
else
echo 'No Categories Found...';