如何显示magento子类的描述

时间:2011-07-14 07:17:54

标签: php magento

我有一个主要类别和此类别的一些子类别,我想在访问主类别链接时显示此类别的子类别。

为此,我创建了一个静态块和一个phtml文件,并能够在主要类别页面上显示子猫名称和图像的子类别,但我还需要显示类别的描述但未能获得描述子类别。

这就是我在phtml文件中的内容

<?php $_categories=$this->getCurrentChildCategories()?>
<?php if($_categories->count()): ?>
<table style="width:80%">
<?php foreach ($_categories as $_category): ?>
<?php if($_category->getIsActive()): ?>

<tr class="<?php echo $this->htmlEscape($_category->getUrlKey()) ?>">
<?php
 if ($_imgUrl = $_category->getImageUrl()) { 
        $_imgHtml = '<img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($_category->getName()).'" />';

    }   
?>  
<td valign="middle" align="left" style="padding-bottom:40px">   
    <a href="<?php echo $this->getCategoryUrl($_category) ?>">
        <?php echo $_imgHtml; ?>
    </a>
</td>
<td valign="top" align="left" >
    <a valign="bottom" href="<?php echo $this->getCategoryUrl($_category) ?>">
        <?php echo $this->htmlEscape($_category->getName()) ?>      
    </a>
    <?php echo $this->htmlEscape($_category->getDescription()) ?>
</td>

</tr>
<?php endif; ?>
<?php endforeach ?>
</table>
<? endif; ?>

但它没有显示子类别的描述,因为你可以看到我使用

$_category->getDescription()获取说明。

请帮忙解决此问题。

2 个答案:

答案 0 :(得分:1)

得到解决方案: -

为了获得子类别的描述,您必须将它们作为当前类别。

所以这段代码应放在foreach循环中

将类别设为当前类别

$cur_category=Mage::getModel('catalog/category')->load($_category->getId());
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);

这就是如何检索描述

<?php echo $this->getCurrentCategory()->getDescription() ?>

答案 1 :(得分:0)

以下是完整的答案:

<?php $_categories=$this->getCurrentChildCategories()?>
                <?php if($_categories->count()): ?>
                <table style="width:80%">
                <?php foreach ($_categories as $_category): ?>
                <?php if($_category->getIsActive()): ?>

                <tr class="<?php echo $this->htmlEscape($_category->getUrlKey()) ?>">
                <?php
                 if ($_imgUrl = $_category->getImageUrl()) { 
                    $_imgHtml = '<img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($_category->getName()).'" />';

                }   
                ?>  
                 <td valign="middle" align="left" style="padding-bottom:40px">   
                 <a href="<?php echo $this->getCategoryUrl($_category) ?>">
                    <?php echo $_imgHtml; ?>
                </a>
                </td>
                <td valign="top" align="left" >
                <a valign="bottom" href="<?php echo $this->getCategoryUrl($_category) ?>">
                    <?php echo $this->htmlEscape($_category->getName()) ?>      
                </a>
                    <?php 
                        $cur_category=Mage::getModel('catalog/category')->load($_category->getId());
                            $layer = Mage::getSingleton('catalog/layer');
                            $layer->setCurrentCategory($cur_category);
                    ?>
                    <?php echo $this->getCurrentCategory()->getDescription() ?>
            </td>

            </tr>
            <?php endif; ?>
            <?php endforeach ?>
            </table>
            <? endif; ?>