簡體   English   中英

如何顯示子類別的描述 magento

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

我有一個主類別和該類別的一些子類別,我想在訪問主類別鏈接時顯示該類別的子類別。

為此,我創建了一個 static 塊和一個 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()獲取描述。

請幫助解決這個問題。

得到了解決方案: -

為了獲得子類別的描述,您必須將它們設為當前類別。

所以這段代碼應該放在foreach循環中

將類別設置為當前類別

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

這就是檢索描述的方式

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

這是完整的答案:

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