2011-08-26 118 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來定義特定的屬性。