Adding Category Image at Top of Category Landing Page in Magento
In this tutorial, we’ll guide you through adding a category image at the top of your category landing page using Magento 1.9 version with the Luxury theme.
Current Status
You’ve already tried adding an image to the “Manage Categories” section under “General Information”. You also attempted modifying the Breadcrumbs.phtml file but didn’t get the desired result. Let’s troubleshoot this issue step-by-step.
Step 1: Understanding the Current Code
Let’s first understand the current code in your breadcrumbs.phtml file:
<?php if($crumbs && is_array($crumbs)): ?>
<?php
$bg_image = $this->getLayout()->createBlock('cms/block')->setBlockId('luxury-background-breadcrumbs')->toHtml();
preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $bg_image, $matches);
?>
<div class="breadcrumbs" <?php if($matches[1][0] != ''): ?>style="background-image:url(<?php echo $matches[1][0]?>);"<?php endif;?>></div>
</div>
The code is currently using a regular expression to extract the image URL from a CMS block called ‘luxury-background-breadcrumbs’. It then applies this background image to the breadcrumbs div.
Step 2: Retrieve Category Image
To display the category image, we need to retrieve it first. Magento stores the category image in the catalog_category_entity_varchar table under the image column.
We’ll create a new block to fetch and display the category image:
-
In your theme directory (e.g.,
app/design/frontend/default/luxury), navigate to thelocal/Mage/Catalog/Block/folder or create it if it doesn’t exist. -
Create a new file named
CategoryView.phpand add the following code:
<?php
class Luxury_Catalog_Block_CategoryView extends Mage_Core_Block_Template
{
public function getCategoryImageUrl()
{
$category = Mage::registry('current_category');
if ($category->getImage()) {
return Mage::helper('catalog/image')->init($category, 'image')->resize(300)->__toString();
}
return false;
}
}
This block Luxury_Catalog_Block_CategoryView fetches the current category from the registry and retrieves its image URL using Magento’s core image helper.
Step 3: Display Category Image on Page
Now, let’s display this image at the top of your category landing page. We’ll make changes to the catalog_category_view.phtml file located in:
app/design/frontend/default/luxury/template/catalog/category/
Add the following code at the beginning of the file, just before the breadcrumbs div:
<?php if ($this->getCategoryImageUrl() != false): ?>
<div class="category-image">
<img src="<?php echo $this->getCategoryImageUrl(); ?>" alt="<?php echo $this->htmlEscape($this->getCategory()->getName()); ?>">
</div>
<?php endif; ?>
This code checks if the category image exists. If it does, an img tag is created to display the image with the category name as its alt text.
Step 4: Add CSS Styling
To style the category image, add the following CSS to your theme’s main CSS file (e.g., skin/frontend/default/luxury/css/styles.css) or create a new one:
.category-image {
padding-top: 50px;
text-align: center;
}
.category-image img {
max-width: 100%;
height: auto;
}
This CSS centers the image and ensures it won’t exceed the container’s width.
Conclusion
After following these steps, you should now see your category image at the top of your category landing page. Make sure to clear your cache if you don’t see immediate changes.
Remember, this solution assumes that an image has been uploaded for the specific category in question. If no image is present, the code will not display anything.
If you’re still facing issues or need further assistance with Magento development, feel free to visit Laravel Company for expert consultations and services tailored to your needs.
Happy coding!