2 magento
嗨,我是magento的新手,并且一直在尝试设置一个静态块,显示一个类别中的子类别列表.我成功地抓住了子类别的图像和名称,但由于某些原因我似乎无法得到描述.
这里的代码无法解释为什么它不起作用以及我如何解决它?
我已经注释了几行,因为我正在尝试不同的方法来让它发挥作用.
助手( '目录/输出'); $ category = $ this-> getCurrentCategory(); getCurrentChildCategories(); ?>Run Code Online (Sandbox Code Playgroud)<?php foreach ($_categories as $_category): ?> <?php echo$这个 - > htmlEscape($ _分类 - > getCategoryDescription());?>
Run Code Online (Sandbox Code Playgroud) ?>"title ="htmlEscape($ _ category-> getName())?>"><?php if($_category->getIsActive()): ?> <div class="subcategory-image"> <a href="<?php echo $_category->getURL()Run Code Online (Sandbox Code Playgroud) ?>"title ="htmlEscape($ _ category-> getName())?>"> htmlEscape($ _ category-> getName())?> getURL()?>"class ="moreLink"> [更多...] getDescription()?> - > getDescription()):?> categoryAttribute($ _ category,$ _description,'description'); ?> - ></a> <?php /* echo "Find this item->" */ ?> </div> <div class="sub-category-container"> <h2><a href="<?php echo $_category->getURL()
Col*_*inM 10
这是Varien决定他们应该在返回数据集之前调用"load"的情况之一,而实际上并不是必需的并且使得实用程序功能完全没用.如果你追踪代码,Mage_Catalog_Block_Navigation->getChildrenCategories()你最终会找到这个在Mage_Catalog_Model_Resource_Eav_Mysql4_Category:
public function getChildrenCategories($category)
{
$collection = $category->getCollection();
/* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
$collection->addAttributeToSelect('url_key')
->addAttributeToSelect('name')
->addAttributeToSelect('all_children')
->addAttributeToSelect('is_anchor')
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
->setOrder('position', 'ASC')
->joinUrlRewrite()
->load();
return $collection;
}
Run Code Online (Sandbox Code Playgroud)
倒数第二行->load();意味着执行查询并加载集合,因此修改查询为时已晚.所以你想要做的是复制和粘贴代替调用getChildrenCategories,然后添加你想要使用的其他属性,如下所示:
$_categories = $category->getCollection()
->addAttributeToSelect(
array('url_key','name','all_children','is_anchor','description')
)
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
->setOrder('position', 'ASC')
->joinUrlRewrite()
;
Run Code Online (Sandbox Code Playgroud)
现在集合将包含description属性,以便getDescription()可以工作.请注意,您不需要调用load(),它会在您开始使用迭代器时自动发生(foreach循环触发此操作).这就是为什么将load()调用包含在该函数中是没有意义的,因为否则你可能只是在函数调用下面添加了一行:
$categories->addAttributeToSelect('description');
Run Code Online (Sandbox Code Playgroud)
但相反,您必须复制函数的内容以调整查询.
| 归档时间: |
|
| 查看次数: |
9837 次 |
| 最近记录: |