1

I want to add images to top navigation menu, I use this code:

app/code/local/Mage/Catalog/Model/Observer.php

        $categoryData = array(
            'name' => $category->getName(),
        'id' => $nodeId,
        'url' => Mage::helper('catalog/category')->getCategoryUrl($category),
        'is_active' => $this->_isActiveMenuCategory($category),
        'thumbnail' => Mage::getModel('catalog/category')->load($category->getId())->getImage()
        );

and this:

app/code/local/Mage/Page/Block/Html/Topmenu.php

    $urls = Mage::getBaseUrl('media').'catalog/category/'.$child->getData('thumbnail');
    $img = '<img class="img-responsive" src="'.$urls.'" />';

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

The images are in the menu and everything work fine but in the frontend I see this error:

"NetworkError: 404 Not Found - http://example.com/media/catalog/category/"

What is wrong?

1
  1. Reindex All.
  2. Check the permission Media folder 775.
  3. Clear your Magento & browser cache.

EDIT:

if (!empty($child->getData('thumbnail'))) {
    $urls = Mage::getBaseUrl('media').'catalog/category/'.$child->getData('thumbnail');
} else {
    $urls = Mage::getBaseUrl('media').'catalog/category/default.png';
}

$img = '<img class="img-responsive" src="'.$urls.'" />';

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

Nb: don't forget to add a some default category image: media/catalog/category/default.png

  • I already do all this but without any success, maybe is because some categories don't have images? – Robert Dec 8 '17 at 21:15
  • Normally no but with your custom maybe, Look my update – PЯINCƏ Dec 8 '17 at 21:47
  • Hi, thank you I test your code now, and I have this error: Fatal error: Can't use method return value in write context in .... – Robert Dec 10 '17 at 13: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.