简单调用获取类别图像&在列表中显示

时间:2012-04-04 10:06:02

标签: php magento imageurl

我按父类别ID显示子类别列表,我希望显示类别图像而不是类别名称。

这是我到目前为止所拥有的......

<div id="menu_brands">
<div class="brand_head">
    <h3><?php echo $this->__('Browse By Brand') ?></h3>
</div>
<div class="brand_list">
    <?php
        $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()] = $category->getUrl();
                $img = $category->getImageUrl(); //I suspect this line is wrong
        }

        ksort($categories, SORT_STRING);
    ?>

        <ul>
            <?php foreach($categories as $name => $url): ?>
                <li>
                    <!--<a href="<?php echo $url; ?>"><?php echo $name; ?></a>-->
                    <a href="<?php echo $url; ?>" title="<?php echo $name; ?>">
                        <img src="<?php echo $img; ?>" width="auto" alt="<?php echo $name; ?>" /> <!--I suspect this line is wrong-->
                    </a>
                </li>
            <?php endforeach; ?>
        </ul>
</div>
</div>

我尝试了无数种方式来显示图像来代替类别名称,但似乎没有任何方法可以显示图像。目前有上述内容,输出是一个空的'img src',所以我正在尝试的内容显然是错误的(并且可能是实现我所追求的更好的方法)。

有人可以指出问题是什么吗?

如果它有任何相关性,那么我打算在之后做的是以网格格式显示类别图像(每行3或4个)。

非常感谢提前。

3 个答案:

答案 0 :(得分:8)

zigojacko的解决方案并不理想,因为它在循环中单独加载模型。这不能很好地扩展,并且许多类别会破坏您的数据库。理想情况下,您可以将图像添加到子集合中。

更快的解决方案是将图像属性添加到具有ID过滤器的集合,如B00MER:

// Gets all sub categories of parent category 'Brands'
$parent = Mage::getModel('catalog/category')->load(6);

// Create category collection for children
$childrenCollection = $parent->getCollection();
// Only get child categories of parent cat
$childrenCollection->addIdFilter($parent->getChildren());
// Only get active categories
$childrenCollection->addAttributeToFilter('is_active', 1);

// Add base attributes
$childrenCollection->addAttributeToSelect('url_key')
        ->addAttributeToSelect('name')
        ->addAttributeToSelect('all_children')
        ->addAttributeToSelect('is_anchor')
        ->setOrder('position', Varien_Db_Select::SQL_ASC)
        ->joinUrlRewrite();

// ADD IMAGE ATTRIBUTE
$childrenCollection->addAttributeToSelect('image');

?>
<ul>
    <?php foreach($childrenCollection as $cat): ?>
        <li>
            <a href="<?php echo $cat->getURL(); ?>" title="<?php echo $cat->getName(); ?>">
                <img class="cat-image" src="<?php echo $cat->getImageUrl(); ?>" />
            </a>
        </li>   
    <?php endforeach; ?>
</ul>

答案 1 :(得分:4)

我们自己设法解决了这个问题 - 请参阅下面的修复。

<?php
    //gets all sub categories of parent category 'Brands'
    $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>

感到惊讶的是,我们没有得到更多支持,因为它是一个相对简单的Magento问题。感谢B00MER的回答。

答案 2 :(得分:2)

尝试使用此方法,而不是$img = $category->getImageUrl();$img = getCategoryImage($category);

function getCategoryImage($category) {
    $categoryCollection = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->getCollection()->addAttributeToSelect('image')->addIdFilter($category->getId());

    foreach($categoryCollection as $category) {
        return $category->getImageUrl();
    }
}

(现已解散)参考:http://www.magentocommerce.com/boards/viewthread/5026/P45/