Php 只需简单的呼叫即可获取类别图像&;在列表中显示
我正在按父类别ID显示子类别列表,并希望显示类别图像以代替类别名称 这是我到目前为止所拥有的Php 只需简单的呼叫即可获取类别图像&;在列表中显示,php,magento,imageurl,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 $
<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个)
非常感谢。请尝试使用此方法,而不要使用
$img=$category->getImageUrl()代码>至$img=getCategoryImage($category)代码>
(现已失效)参考:我们自己设法解决了这个问题-请参阅下面的修复
<?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的回答。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>
//获取父类别“品牌”的所有子类别
$parent=Mage::getModel('catalog/category')->load(6);
//为儿童创建类别集合
$childrenCollection=$parent->getCollection();
//仅获取父猫的子类别
$childrenCollection->addIdFilter($parent->getChildren());
//仅获取活动类别
$childrenCollection->addAttributeToFilter('is_active',1);
//添加基本属性
$childrenCollection->addAttributeToSelect('url\u key')
->addAttributeToSelect('名称')
->addAttributeToSelect(“所有子项”)
->addAttributeToSelect('is_anchor')
->setOrder('position',Varien\u Db\u Select::SQL\u ASC)
->joinUrlRewrite();
//添加图像属性
$childrenCollection->addAttributeToSelect('image');
?>
-
致命错误:调用第49行“$img=getCategoryImage($category);”中//路径//中未定义的函数getCategoryImage()(第49行)函数是否应该在同一个文件中声明?目前,它们与所有其他文件一起位于相关的extensions Navigation.php中。谢谢谢谢罗伯特,这个比较优雅。这个很好用,谢谢罗伯特!对于任何想要获取缩略图而不是类别的人,请使用$childrenCollection->addAttributeToSelect(“缩略图”)
然后在foreach
// 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>