简体   繁体   English

如何显示子类别的描述 magento

[英]how to show the description of sub-categories magento

I have a main category and some sub-categories for this category and I wanted to show the sub categories of this category when main category link is accessed.我有一个主类别和该类别的一些子类别,我想在访问主类别链接时显示该类别的子类别。

For this I have created a static block and one phtml file and able to show the sub-categories on main category page with sub-cat name and image but I also needs to show the description of category but failed to get the description of sub categories.为此,我创建了一个 static 块和一个 phtml 文件,并且能够在主类别页面上显示带有子类别名称和图像的子类别,但我还需要显示类别的描述,但未能获得子类别的描述.

This is what I have in phtml file这就是我在 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; ?>

But it is not showing description for the sub categories as you can see I used但它没有显示子类别的描述,如您所见,我使用了

$_category->getDescription() to get the description. $_category->getDescription()获取描述。

Please help to resolve this.请帮助解决这个问题。

Got the solution:-得到了解决方案: -

In order to get the description of sub-categories you have to make them current category.为了获得子类别的描述,您必须将它们设为当前类别。

so this code should be placed inside the foreach loop所以这段代码应该放在foreach循环中

Set the category as current category将类别设置为当前类别

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

and this is how then retrieve description这就是检索描述的方式

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

Here is the full answer:这是完整的答案:

<?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; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM