【问题标题】:Simple Call to Get Category Images & Display in List简单调用获取类别图像并在列表中显示
【发布时间】:2012-04-18 00:52:49
【问题描述】:

我正在按父类别 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 个)显示类别图像。

非常感谢。

【问题讨论】:

    标签: php magento imageurl


    【解决方案1】:

    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>
    

    【讨论】:

    • 感谢罗伯特,这更优雅了。
    • 这很好用,谢谢罗伯特!对于可能想要获取缩略图而不是类别的任何人,请使用$childrenCollection-&gt;addAttributeToSelect('thumbnail');,然后在foreach 中调用$cat-&gt;getThumbnail()
    【解决方案2】:

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

    <?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 的回答。

    【讨论】:

      【解决方案3】:

      试试这个方法,而不是你的$img = $category-&gt;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/

      【讨论】:

      • 致命错误:在第 49 行的 //path// 中调用未定义函数 getCategoryImage() '$img = getCategoryImage($category);' (是第 49 行)
      • 应该在同一个文件中声明函数吗?目前,它们与所有其他扩展一起位于相关扩展 Navigation.php 中。谢谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多