php 简单调用以获取类别图像并显示在列表中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10009012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple Call to Get Category Images & Display in List
提问by zigoHymano
I'm displaying a list of sub categories by parent category ID and am wanting to display the category image in place of a category name.
我正在按父类别 ID 显示子类别列表,并且想要显示类别图像来代替类别名称。
Here is what I have so far...
这是我到目前为止...
<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>
I've tried countless ways to display the images in place of the category names but nothing seems to make the images appear. Currently with the above, the output is an empty 'img src' so there is clearly an error with what I'm trying (and probably a better way of achieving what I'm after).
我尝试了无数种方法来显示图像来代替类别名称,但似乎没有任何方法可以使图像出现。目前,输出是一个空的“img src”,因此我正在尝试的内容显然存在错误(并且可能是实现我所追求的更好的方法)。
Please could someone kindly point out what the problem is?
请有人可以指出问题是什么吗?
If it's of any relevance, what I intend to do afterwards is then display the category images in a grid format (3 or 4 per line).
如果它有任何相关性,我之后打算做的是以网格格式(每行 3 或 4 个)显示类别图像。
Many thanks in advance.
提前谢谢了。
回答by Robert Egginton
The solution by zigoHymano is not ideal because it separately loads in models in a loop. This does not scale well, and with many categories will thrash your database. Ideally, you'd add the images to the child collection.
zigoHymano 的解决方案并不理想,因为它在循环中单独加载模型。这不能很好地扩展,并且许多类别会影响您的数据库。理想情况下,您应该将图像添加到子集合中。
A faster solution is to add the image attribute to a collection with an ID filter like B00MER:
更快的解决方案是将图像属性添加到具有 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>
回答by zigoHymano
We managed to resolve this ourselves - see fix below.
我们设法自己解决了这个问题 - 请参阅下面的修复。
<?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>
Surprised we didn't get more support on this with it being a relatively simple Magento issue. Thanks B00MER for your answer though.
很惊讶我们没有得到更多的支持,因为它是一个相对简单的 Magento 问题。不过感谢 B00MER 的回答。
回答by B00MER
Give this method a try instead of your $img = $category->getImageUrl();to $img = getCategoryImage($category);
试试这个方法而不是你$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();
}
}
(now defunct) Reference: http://www.magentocommerce.com/boards/viewthread/5026/P45/
(现已失效)参考:http: //www.magentocommerce.com/boards/viewthread/5026/P45/

