目前我有一个子类别页面,显示正确的名称,拉出正确的链接并以有序的方式显示所有内容。
然而,我遇到了一个特殊问题,因为图像都是从其中一个类别中提取的相同图片。这当然应该是与类别名称相关的图像,任何人都可以找到我犯了错误的地方吗?
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $categoryId = 465;?>
<?php $category = Mage::getModel('catalog/category')->load($categoryId) ?>
<?php $_categories = $category->getChildrenCategories() ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php if($_category->hasChildren()):?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<ul class="catblocks">
<?php foreach($_subcategories as $_subcategory): ?>
<li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>"><?php echo $_subcategory->getName() ?></a>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>"><img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $category->getImage() ?>" alt="<?php echo $_subcategory->getName() ?>" />
<span><?php echo $_subcategory->getName() ?></span></a>
<?php $_category2 = Mage::getModel('catalog/category')->load($_subcategory->getId()) ?>
<?php if($_category2->hasChildren()):?>
<?php $_subcategories2 = $_category2->getChildrenCategories() ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
任何帮助都是apreciated:)
答案 0 :(得分:1)
<?php
//gets all sub categories of parent category
$cats = Mage::getModel('catalog/category')->load(6)->getChildren();
$catIds = explode(',',$cats);
$categories = array();
foreach($catIds as $catId) {
$category = Mage::getModel('catalog/category')->load($catId);
$categories[$category->getName()] = array(
'url' => $category->getUrl(),
'img' => $category->getImageUrl()
);
}
ksort($categories, SORT_STRING);
?>
<ul>
<?php foreach($categories as $name => $data): ?>
<li>
<a href="<?php echo $data['url']; ?>" title="<?php echo $name; ?>">
<img class="cat-image" src="<?php echo $data['img']; ?>" />
</a>
</li>
<?php endforeach; ?>
</ul>