1

In my magento shop I don't want to display icons for subcategories in topmenu. But I wan't to leave them in top menu. If I don't select icon for subcategory I get missing img include. You can see screenshot below.

enter image description here

Code that generates output is:

$urls = Mage::getBaseUrl('media').'catalog/category/'.$child->getData('thumbnail');

        $html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>' . '<img src="'.$urls.'" style="width:20px; height:20px;"/>  '
            . $this->escapeHtml($child->getName()) . '</span></a>';

        if ($child->hasChildren()) {
            if (!empty($childrenWrapClass)) {
                $html .= '<div class="' . $childrenWrapClass . '">';
            }
            $html .= '<ul class="level' . $childLevel . '">';
            $html .= $this->_getHtml($child, $childrenWrapClass);
            $html .= '</ul>';

            if (!empty($childrenWrapClass)) {
                $html .= '</div>';
            }
        }
        $html .= '</li>';

Thanks for all suggestions.

2

Thanks Marius for pointing me to the right way. Your code needs some improvements, because img src still remains in cart. But code below solved my problem:

        if ($child->getData('thumbnail')){ //check if there is an image
            $html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>' . '<img src="'.$urls.'" style="width:20px; height:20px;"/>  '
            . $this->escapeHtml($child->getName()) . '</span></a>'; //build the img tag
        }
        else {
            $html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>'
            . $this->escapeHtml($child->getName()) . '</span></a>';
        }

Thanks for helping me!

  • You can mark this as accepted if it is the working solution. – David Manners Jan 13 '14 at 21:15
1

Change your code to this:

if ($child->getData('thumbnail')){ //check if there is an image
    $urls = '<img src="'.Mage::getBaseUrl('media').'catalog/category/'.$child->getData('thumbnail').'" style="width:20px; height:20px;" />'; //build the img tag
}
else {
    $urls = ''; //no image = no img tag
}

$html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>' . $urls . $this->escapeHtml($child->getName()) . '</span></a>';

if ($child->hasChildren()) {
    if (!empty($childrenWrapClass)) {
        $html .= '<div class="' . $childrenWrapClass . '">';
    }
    $html .= '<ul class="level' . $childLevel . '">';
    $html .= $this->_getHtml($child, $childrenWrapClass);
    $html .= '</ul>';
    if (!empty($childrenWrapClass)) {
        $html .= '</div>';
    }
}
$html .= '</li>';

I hope I didn't miss a quote or double quote.

  • With a little modification of your code I did solve my problem. I will post full answer later, because my reputation is too low to post solution so quick. – Iztok Dec 13 '13 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.