简体   繁体   中英

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.

This is what I have in phtml file

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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