【问题标题】:Get name and image from a list of sub categories in Magento从 Magento 的子类别列表中获取名称和图像
【发布时间】:2012-10-04 00:34:43
【问题描述】:

我正在尝试获取 Magento 类别的所有子类别并显示它们的名称和图像。

到目前为止我有:

$children = Mage::getModel('catalog/category')->getCategories(28);

foreach ($children as $category) {
    echo "name: " . $category->getName() . "<br>"; 
    echo "image: " . $category->getImageUrl() . "<br>"; 
}

$category-&gt;getName() 有效,但 $category-&gt;getImageUrl() 似乎无效。

我正在关注目录/类别模型的Magento docs 参考(请参阅顶部的 Mage:Catalog 和侧面的 Mage_Catalog_Model_Category)。

它明确指出string getImageUrl () 是该类的可用方法,但我显然不能调用它。

我想知道为什么在上面这个方法不可用,更一般地说,为什么很多方法似乎在一个类中不可用,所以我可以理解如何解决这些反复出现的问题未来。

所以,我的问题是,以上内容需要什么才能使 getImageUrl 方法可用,并且,通常我如何确定需要添加哪些内容才能使文档中的其他方法在“默认”中不可用Magento 框架?

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: php magento magento-1.7


    【解决方案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>
    

    【讨论】:

    • 嗨,谢谢,这行得通......你知道是否有一种方法可以让你不必从 IDs 字符串手动构建数组?我确定 Magento 有一种方法可以在一次调用中获取所有数据?...这也很有帮助,干杯。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多