Displaying Magento Category Images in Your Top Menu
One of the most common requests we receive from our clients at Laravel Company is how to display category images instead of just names in their top menu navigation. This adds visual appeal and improves user experience by providing thumbnail previews of what each section contains.
Understanding Magento’s Category Image Storage
In Magento, category images are stored under media/wysiwyg/prototype/extjs/resources/images/default/samples. By default, the system only provides a few sample images for demonstration purposes. To add your own images, you’ll need to upload them and assign them to specific categories.
Updating Your Category Image
To upload a new image:
- Navigate to
Content > Design > Configuration. - Select your website from the dropdown menu.
- Click “Edit” under the “Actions” column for your store view.
- Scroll down to the “Miscellaneous Design Layout XML” field.
- Add or update the following code:
<reference name="header">
<action method="setTemplate">
<template>page/html/header.phtml</template>
</action>
</reference>
This will apply your custom header template.
- Click “Save Configuration”.
Customizing the Top Menu Template
Next, you’ll need to modify the top menu template located at app/design/frontend/{package}/{theme}/template/page/html/topmenu.phtml. This file controls how your category links are rendered.
In your top menu template, locate the following line of code:
<a href="<?php echo $this->getUrl('catalog/category/view', array('id'=>$category->getId())); ?>"><?php echo $this->escapeHtml($category->getName()); ?></a>
Replace it with this PHP snippet:
<?php if ($category->getImage()): ?>
<div class="topmenu-image">
<img src="<?php echo $this->helper('catalog/image')->init($category, 'image'); ?>" alt="<?php echo $this->escapeHtml($category->getName()); ?>">
</div>
<a href="<?php echo $this->getUrl('catalog/category/view', array('id'=>$category->getId())); ?>"><?php echo $this->escapeHtml($category->getName()); ?></a>
<?php else: ?>
<a href="<?php echo $this->getUrl('catalog/category/view', array('id'=>$category->getId())); ?>"><?php echo $this->escapeHtml($category->getName()); ?></a>
<?php endif; ?>
This code checks if the category has an image. If it does, it displays a thumbnail of that image alongside the category name.
Styling Your Category Images
Finally, you’ll want to add some basic styling for your new category images. In your CSS file (usually skin/frontend/{package}/{theme}/css/styles.css), add the following:
.topmenu-image img {
float: left;
margin-right: 10px;
width: 50px;
height: auto;
}
This will ensure that your category images are displayed at a consistent size and alignment.
Conclusion
Displaying Magento category images in your top menu navigation is an excellent way to enhance user experience through visual cues. By following the steps outlined above, you can easily upload new images, assign them to categories, and display them alongside your category names in the menu.
As always, if you need expert assistance with any aspect of Magento development or e-commerce strategy, our team at Laravel Company is here to help. You can reach out to us through our website https://laravelcompany.com/contact-us/.