Magento – How to display category image in top navigation menu

magento-1.9

I have used below code to override category menu but unable to add category image in top menu.

protected function _getHtml(Varien_Data_Tree_Node $menuTree, $childrenWrapClass) {

    $html = '';

    $children = $menuTree->getChildren();

    $parentLevel = $menuTree->getLevel();
    $childLevel = is_null($parentLevel) ? 0 : $parentLevel + 1;

    $counter = 1;
    $childrenCount = $children->count();

    $parentPositionClass = $menuTree->getPositionClass();
    $itemPositionClassPrefix = $parentPositionClass ? $parentPositionClass . '-' : 'nav-';

    foreach ($children as $child) {
        $child->setLevel($childLevel);
        $child->setIsFirst($counter == 1);
        $child->setIsLast($counter == $childrenCount);
        $child->setPositionClass($itemPositionClassPrefix . $counter);

        $outermostClassCode = '';
        $outermostClass = $menuTree->getOutermostClass();

        if ($childLevel == 0 && $outermostClass) {
            $outermostClassCode = ' class="' . $outermostClass . '" ';
            $child->setClass($outermostClass);
        }
        $html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>';
        $html .= '<a  href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>'
                . $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>';

        $counter++;
    }

    return $html;
}

i have applied many solutions but no luck. Can anyone help?

Best Answer

Hi finally i got the answere:

        $splittedArray = explode($delimiter = 'category-node-', $child->getId());
        $category = Mage::getModel('catalog/category')->load(end($splittedArray));
        $html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>';
        if (!empty($category->getData('image'))) {
            $html .= '<div> ';
            $html .= "<a href=" . $category->getURL() . " title =" . $category->getName() . ">";
            $html .= "<img src=" . Mage::getBaseUrl('media') . 'catalog/category/' . $category->getData('image') . " alt=" . $category->getName() . " >";
            $html .= "</a>";
            $html .= '</div>';
        }