2011-08-26 122 views
-1

我在Magento中获得了子类别列表的类别页面。该列表具有图像和名称,但我也想显示这些子类别的描述。我试图简单地添加Magento在类别list.phtml上显示子类别描述

<strong><?php echo $this->htmlEscape($_category->getDescription()) ?></strong> 

但它不工作。

编辑:我得到了一个传统的方式小类:

<?php if (!$_categoryCollection->count()): ?> 
<p class="note-msg"><?php echo $this->__('There are no subcategories matching the selection.') ?></p> 
<?php else: ?> 
<div class="category-products"> 
    <?php $_collectionSize = $_categoryCollection->count() ?> 
    <?php $_columnCount = $this->getColumnCount(); ?> 
    <?php $i=0; foreach ($_categoryCollection as $_category): ?> 
     <?php if ($i++%$_columnCount==0): ?> 
     <ul class="products-grid"> 
     <?php endif ?> 
      <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>"> 
       <a href="<?php echo $_category->getUrl() ?>" class="product-image"><img class="photo" src="<?php echo $this->getCategoryImage($_category, 214, 184); ?>" width="214" height="184" alt="<?php echo $_category->getName() ?>" /> 
       <strong><?php echo $this->htmlEscape($_category->getName()) ?></strong> 
       <strong><?php echo $_category->getDescription() ?></strong> 
       </a> 
      </li> 
     <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?> 
     </ul> 
     <?php endif ?> 
    <?php endforeach ?> 
</div> 

我已经尝试通过增加->addAttributeToSelect(’description’)以更新category.php文件中的公共职能getChildrenCategories($category),但它不工作。

+0

你如何得到子类?请添加更多代码... – Simon

回答

3

我看不出究竟是什么,你做错了,但也许我仍然可以帮助。我已经成功地在list.phtml中显示了子类别的描述,并且您可能可以根据自己的用途调整对我有用的内容。以下是这对我的作品的代码的精简版本:

<?php $children = explode(",", $this->getCurrentCategory()->getChildren()); ?> 
<div class="category-products"> 
    <ul class="products-grid"> 
     <?php foreach($children as $child): ?> 
      <?php $_child = Mage::getModel('catalog/category')->load($child); ?> 
      <li class="item"><?php echo $_child->getDescription(); ?></li> 
     <?php endforeach; ?> 
    </ul> 
</div> 

你在做什么,我上面的示例是目录模型对象上的getChildren()方法返回的最大区别然后使用类别Ids来加载相应的子类别模型实例。我的记忆可能在这里是错误的,但我似乎记得从Magento收集返回的项目不包含您通过id加载时获得的完整数据。

我不知道这是否会显著或不会影响性能(我会假设,加载一个集合比加载单个车型速度更快),但它的作品,所以我不是在抱怨......

希望这帮助。

干杯, 扎克

0

我有比较网站我的工作,在那里我在网格视图中显示的子类别相同的想法。虽然我曾经使用通过id的单独加载类别/产品信息的方法,但我有点爱上使用“Mage :: getModel('') - > getCollection()”方法。

这就是我对我的子类使用,我已经与它很高兴,因为它一下子抓住所有的信息:

<?php 
    if(Mage::registry('current_category')->getId()) { 

     $_currentCategoryId = Mage::registry('current_category')->getId(); 

    } else { //Get Root Category Id if not in a category 

     $_currentCategoryId = Mage::app()->getStore()->getRootCategoryId(); 
    } 

    $_subCategories = Mage::getModel('catalog/category')->getCollection() 
         ->addAttributeToSelect('*') 
         ->addFieldToFilter('parent_id',array('eq' => $_currentCategoryId)) 
         ->addFieldToFilter('include_in_menu',array('eq' => '1')) 
         ->addFieldToFilter('is_active', array('eq' => '1')) 
         ->addAttributeToSort('position', 'asc'); 
?> 
<?php if(count($_subCategories) > 0): ?> 

<!--This is where the sub-category layout would go.--> 

<?php else: ?> 

<!--Do something else if there are no sub-categories.--> 

<?php endif; ?> 

这将抓住所有可见子类别或者抓取商店的基本类别(从根类别标识),如果您选择将模板显示在任何其他页面上。你可以进一步使用addAttributeToSelect来定义特定的属性。