隐藏类别标题属性,如果它是空的Magento


Hide category heading attribute if it's empty in Magento

有人能帮我一下吗?我们在下面的代码中显示产品所属的类别列表(我们使用的类别属性是"creareseo_heading")。然而,有时"creareseo_heading"属性是空的,但该行仍然显示一个标签,但没有值。我认为我们需要某种"if"语句来完全隐藏行。我什么都试过了,就是不行。基本上,如果"creareseo_heading"类别属性列表为空,则需要该行完全消失。

解决! !见下文:)

<?php
    $_helper = $this->helper('catalog/output');
    $_product = $this->getProduct();
    $currentCatIds = $_product->getCategoryIds();
    $catCollection = Mage::getResourceModel('catalog/category_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('entity_id', $currentCatIds)
    ->addAttributeToFilter('is_active', 1)
    ->setOrder('creareseo_heading', 'asc')
    ->addIsActiveFilter();
?>
<?php if($_additional = $this->getAdditionalData()): ?>
    <h2><?php echo $this->__('Additional Information') ?></h2>
    <table class="data-table" id="product-attribute-specs-table">
        <col width="25%" />
        <col />
        <tbody>
        <?php foreach ($_additional as $_data): ?>
        <?php $_attribute = $_product->getResource()->getAttribute($_data['code']);
        if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?>
            <tr>
                <th class="label"><?php echo $this->escapeHtml($this->__($_data['label'])) ?></th>
                <td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
            </tr>
        <?php } ?>
        <?php endforeach; ?>
        <?php
            $applicationGuides = array();
            foreach($catCollection as $cat){
                $c = $cat->getCreareseoHeading();
                if (!empty($c)) $applicationGuides[] = $c;
            }
        ?>
        <?php if(!empty($applicationGuides)): ?>
            <tr>
                <th class="label"><?php echo $this->__('Application Guide') ?></th>
                <td class="data">
                    <ul>
                    <?php foreach($applicationGuides as $cat): ?>
                        <li><?php echo $cat; ?></li>
                    <?php endforeach; ?>
                    </ul>
                </td>
            </tr>
            <?php endif;?>
        </tbody>
    </table>
    <script type="text/javascript">decorateTable('product-attribute-specs-table')</script>
<?php endif;?>
        <?php
            /* Build list with no empty values */
            $applicationGuides = array();
            foreach($catCollection as $cat){
                $c = $cat->getCreareseoHeading();
                if (!empty($c)) $applicationGuides[] = $c;
            }
        ?>
        <?php if(!empty($applicationGuides)): ?>
        <tr>
            <th class="label"><?php echo $this->__('Application Guide') ?></th>
            <td class="data">
                <ul>
                <?php foreach($applicationGuides as $cat): ?>
                    <li><?php echo $cat; ?></li>
                <?php endforeach; ?>
                </ul>
            </td>
        </tr>
        <?php endif;?>

这对于注释来说太长了,所以添加另一个答案来帮助您调试问题…

是否报告了任何错误?如果您理解代码,也许您可以对代码进行逐步更改,看看是否得到预期的结果。如果你不是,那么我建议你开始添加这个代码块到你的原始代码,就在我们正在谈论的<tr>块之上…

        <?php
            /* Build list with no empty values */
            $applicationGuides = array();
            foreach($catCollection as $cat){
                $c = $cat->getCreareseoHeading();
                if (!empty($c)) $applicationGuides[] = $c;
            }
            var_dump($applicationGuides);
        ?>