如何根据代码的类别级别更改类别页面的模板?
题
我有一个没有模式的项目,我应用了管理区域中类别布局的所有更改(Catalog->Manage Categories->Custom Design
标签)。现在我需要以下内容:
父类别将具有所有子类别的网格,其缩略图和标题。
子类别看起来几乎是默认的类别 - 名称,描述和产品列表/网格
因此,我想这样做,想知道正确的方法是什么。
解决方案
在你的 catalog/category/view.phtml
您可以使用以下方式检查类别级别:
$this->getCurrentCategory()->getLevel()
如果您想在后端决定这一点,则可以制作一个整个单独的模板,以显示所有子类别,并将以下内容添加到您的自定义布局更新中:
<reference name="category.products">
<action method="setTemplate"><template>catalog/category/list.phtml</template></action>
</reference>
要读取所有子类别,您可以做到这一点:
$children = explode(',', $this->getCurrentCategory()->getChildren());
$collection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addAttributeToFilter('entity_id', array(
'in' => $children
))->load();
foreach ($collection as $category) {
echo $category->getName() . '<br />';
}
其他提示
您应该能够将IF块放在高度视图模板的顶部。
就像是:
<?php if ($this->IsTopCategory()) : ?>
// Insert subcategory grid logic here
<?php endif; ?>