我在整个页面中使用这个$categories
$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributetoSelect('*')
->addAttributetoFilter('level',2)
->addisActiveFilter()
->addAttributetoSort('position');
foreach($categories as $cat) {$children=$cat->getChildrenCategories();}
选项1
//option 1
$children = $category->getChildren();
foreach(explode(',', $children) as $child):
$sub = Mage::getModel('catalog/category')->load($child);
$sub->getName() // this return ok, name show up
$sub->getimageUrl() // this return ok, image show up
选项2工作但由于某种原因无法获取图像URL.
//option 2
$children = $category->getChildrenCategories();
foreach($children as $sub):
$sub->getName() // this return ok, name show up
$sub->getimageUrl() // this return empty, image NOT show up so is other attribute beside name.
谁能解释一下这个区别?我将如何选择2来解决这个问题
解决方法:
基本上,当我们使用getChildrenCategories()函数时,只从类别字段集合中检索了几个字段 – > url_key,name,all_children,is_anchor.
你可以在类Mage_Catalog_Model_Resource_Category上看到它.如果想从函数中获取tget图像url那么只需添加addAttributetoFilter(‘image’)
$collection = $category->getCollection();
$collection->addAttributetoSelect('url_key')
->addAttributetoSelect('name')
->addAttributetoSelect('all_children')
->addAttributetoSelect('is_anchor')
->setorder('position', Varien_Db_Select::sql_ASC)
->joinUrlRewrite()
->addAttributetoFilter('is_active', 1)
->addIdFilter($category->getChildren())
->addAttributetoSelect('image');
foreach($category as $eachChildCat){
if ($image = $eachChildCat->getimage()) {
$url = Mage::getBaseUrl('media').'catalog/category/'.$image;
}
}
$eachChildCat-> getimage()不起作用然后使用$eachChildCat-> getResource() – > getimage()