Magento: Custom Category Images Listing Block Tutorial
Recently, I just finished coding out a website for a client in Magento. You can view the site at www.3graces.com, it was a full switch from some old legacy asp shopping cart to Magento. Im really happy with the way things turned out and the client is too, Magento just has so many wonderful features built-in.
One feature that I found wasn’t built in was a way to display each sub-category with an image, and its name on a landing page. Magento has a nice feature which allows you to assign an image to a category, but doesnt give you a way to make that show up in a listing somewhere.
I setup 6 main categories, with each having multiple sub-categories and I wanted to have a way to show all those sub-categories to the user on a nice page that was pleasing to the eye. Here is an example: Payot Paris by Category.
To achieve this layout, I created a block called sub_navigation.html and placed it in the following directory: app/design/frontend/3graces/default/template/catalog/navigaton/sub_navigation.html
<div id="categories">
<div class="col_full">
<div class="listing" >
<?php $_maincategorylisting=$this->getCurrentCategory()?>
<?php $_categories=$this->getCurrentChildCategories()?>
<?php if($_categories->count()):?>
<? foreach ($_categories as $_category):?>
<? if($_category->getIsActive()):
$cur_category=Mage::getModel('catalog/category')->load($_category->getId());
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
$catName = $this->getCurrentCategory()->getName();
if($_imageUrl=!$this->getCurrentCategory()->getImageUrl()):?>
<div class="category-box">
<div class="category-image-box">
<a href="<?php echo $this->getCategoryUrl($_category)?>"><img src="/skin/frontend/3graces/default/images/category_image_default.gif"></a>
</div>
<div class="category-name">
<p><a href="<?php echo $this->getCategoryUrl($_category)?>">
<?php echo $catName ?></a></p>
</div>
</div>
<?endif?>
<? if($_imageUrl=$this->getCurrentCategory()->getImageUrl()):?>
<div class="category-box">
<div class="category-image-box">
<a href="<?php echo $this->getCategoryUrl($_category)?>"><img src="<?php echo $_imageUrl?>" height="80"></a>
</div>
<div class="category-name">
<p><a href="<?php echo $this->getCategoryUrl($_category)?>"> <?php echo $_category->getName()?></a></p>
</div>
</div>
<?
endif;
endif;?>
<?endforeach?>
<?php /* This resets the category back to the original pages category
**** If this is not done, subsequent calls on the same page will use the last category
**** in the foreach loop
*/ ?>
<?php $layer->setCurrentCategory($_maincategorylisting); ?>
<?endif;?>
</div>
<br clear=all>
</div>
</div>
If you notice, I placed an if statement in the code that first checks if an image has been set for the category. If one has, it will show it – if there isn’t an image it will show a default image that I have chosen. Here is an example, when only 1 image for a category has been setup, and the rest are the default.
To make the block show up as an option in Magento Admin, you will need to go to CMS > Static Blocks. Once you get there, click Add New Block and fill in the following fields.
Block Title: Sub Category Listing
Identifier: sub_category_listing
Status: Enabled
Content: {{block type=”catalog/navigation” template=”catalog/navigation/sub_category_listing.phtml”}}
Once you have added all those fields, you can click save. Now to enable this static block for a parent category that contains a bunch of sub-categories with images that are set.
Go to: Catalog > Manage Categories
Then click on the parent category to which you wish to add this layout, scroll to the bottom of the first tab “General Information”.
You will see the following fields, just setup as follows:
Display Mode: Static Block Only
CMS Block: Sub Category Listing
Is Anchor: Yes
Now if you go back to your category on the website, you should see your newly setup sub category image listing page. If you arent seeing any images, make sure you setup you default image and placed it in the current directory within your magento install.
Thanks!
Comments
Hi Jake,
Thank you for posting this. Love it. One SEO question. Currently the images alt tags are populated by the Name filed from the catagory. How can I change it to be populated by the Page Title filed?
Does this work on version 1.3.4?
for anyone still trying to figure out how to align them horizontally, get your CSS skills up to date.
The classes and ID's used, put those in your theme CSS file, probably style.css and make your changes.:
/*=======================================*/
/* category images */
.category-box{
float:left;
}
.category-image-box{
}
.category-name{
}
.listing{
}
.col_full{
}
#categories{
}
Can any one provide the exact step for displaying in horizontally
hi guys
i have somme probleme in layout and i want someone explain me how can i create a nwe block in magento
with my best wishes to you
Awesome It works Perfect Thanks for writing such a nice tutorial.
For those Still getting error plz remove old comma and type comma again in block code given below :
{{block type="catalog/navigation" template="catalog/navigation/sub_navigation.phtml"}}
thank you it works great but I am no CSS guy so I need some help to get these Horizontal
please can someone help
Anyone already got the horizontal grid working?
I guess you need to read up on CSS, but i'll try to push you in the right direction. Here is what you could think of.
The <div class="category-box"> groups together the image <div class="category-image-box"> with the text <div class="category-name">
So if you use the css float:left; inside styles.css (/skin/frontend/yourskin/default/css/styles.css) you will effectively place the group left aligned, whereas the next group is placed immediately to the right (left adjusted) – and so on. When there is no more room left on that row, the next box will fall down the second row. Example below also define a specified width so every box is the same and all columns will be aligned. You should also consider defining a specific height as well and some margin and maybe a nice background image:
.category-box{
width:170px;
height:200px;
margin-right:6px;
float:left;
}
This is exactly what i was searching for. Magneto seems to do everything, but most it just wrong. And the easiests things it can't even do.
Whatever. Some things i wanted to say about this tutorial:
1. You wrote in the before the code block that we should create files with .html suffix which should be .phtml
2. The one thing with the skin url method which was already mentioned but is still unchanged
and 3.
YOU have one of the worst code styling ever.
What i wrong with you opening a new php tag for each line of your code? I once say a php software which echoed each an every line of html in a single echo. This was worst and i thought it would never happen to me again but now…
I fixed the code and added some comments to it + some indention.
And here it is: http://pastebin.com/6ct87kxh
Thanks for your post anyway. This is a great thing and i really apprechiate your work :)
Greetings
func0der
Hello,
I try your code, and.. I Think that is work, but he find no pictures.. (URL failure ? )
Where must I specify the picture for each category.. ?
Thank you..
funcoder,
Not too nice. You start with WOW! This is just what I need and end with you write horrible code. News flash, all you did was tidy it up. So, he WROTE the code and you tidy it up like a maid and you give him a hard time?!?!? ROFL!
you can look at http://www.about-magento.com/magento-create-block… if you want to know more about blocks ;)
Pierre.
Hello, How i cam set all categories in vertical menu like http://www.flipkart.com
…
pls help me to make that kind <mega >menu
I am very new to this. I am using v1.6.2 Magento. I have used the code from this site tutorial, and from another post that said they cleaned it up (http://pastebin.com/6ct87kxh ) and I still cannot get it to work (neither version). I retyped my quote marks as suggested in several posts and I went from nothing showing up on my page to having the actual page code (code for the sub_category_listing.phtml) showing where the sub-categories should be. Any help would be greatly appreciated. I don't know what I have done wrong…
Thanks. It worked great!
Hello !
It's working on 1.6.1 with this code in the .Phtml file :
<?php foreach ($this->getCurrentChildCategories() as $_category): ?>
<?php if($_category->getIsActive()):
$_category = Mage::getModel('catalog/category')->load($_category->getId());
?>
<div class="category-list-view">
<a href="<?php echo $this->getCategoryUrl($_category)?>" title="<?php echo $_category->getName()?>">
<img src="<?php echo Mage::getBaseUrl('media').'catalog/category/'.$_category->getThumbnail(); ?>" height="200px" width="200px" alt="<?php echo $this->htmlEscape($_category->getName()) ?>" />
<h3><a href="<?php echo $this->getCategoryUrl($_category)?>" class="name" title="<?php echo $_category->getName()?>"><?php echo $_category->getName()?></h3>
</div>
<?php endif; ?>
<?php endforeach; ?>
But, .. How can I an horizontal display.. ?..
Thank you.. !
Hi Glenn
I am using magento 1.6.2.0 my filename sub_category_listing.phtml under /app/design/frontend/base/default/template/catalog/navigation. block identifier sub_category_listing and content {{block type=”catalog/navigation” template=”catalog/navigation/sub_category_listing.phtml”}}.
NOTHING DISPLAY ON HOME PAGE
Code Here
<div id="categories">
<div class="col_full">
<div class="listing" >
<?php $_maincategorylisting=$this->getCurrentCategory()?>
<?php $_categories=$this->getCurrentChildCategories()?>
<?php if($_categories->count()):?>
<? foreach ($_categories as $_category):?>
<? if($_category->getIsActive()):
$cur_category=Mage::getModel('catalog/category')->load($_category->getId());
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
$catName = $this->getCurrentCategory()->getName();
if($_imageUrl=!$this->getCurrentCategory()->getImageUrl()):?>
<div class="category-box">
<div class="category-image-box">
<a href="<?php echo $this->getCategoryUrl($_category)?>"><img src="/skin/frontend/3graces/default/images/category_image_default.gif">
</div>
<div class="category-name">
<a href="<?php echo $this->getCategoryUrl($_category)?>">
<?php echo $catName ?>
</div>
</div>
<?endif?>
<? if($_imageUrl=$this->getCurrentCategory()->getImageUrl()):?>
<div class="category-box">
<div class="category-image-box">
<a href="<?php echo $this->getCategoryUrl($_category)?>"><img src="<?php echo $_imageUrl?>" height="80">
</div>
<div class="category-name">
<a href="<?php echo $this->getCategoryUrl($_category)?>"> <?php echo $_category->getName()?>
</div>
</div>
<?
endif;
endif;?>
<?endforeach?>
<?php /* This resets the category back to the original pages category
**** If this is not done, subsequent calls on the same page will use the last category
**** in the foreach loop
*/ ?>
<?php $layer->setCurrentCategory($_maincategorylisting); ?>
<?endif;?>
</div>
</div>
</div>
I am using subcategory_listing.phtml and all works fine except for one problem. When I delete a category a gap appears on the front end images where the deleted category used to be. I have tried every which way to figure this out but am really stuck. The categories are definitely gone even in the database but is seems that they are leaving ghosts which are making the category pages a bit untidy.
Anyone have any ideas?
I have found a fix for the above by re-arranging the sub categories. For some reason if the categories are in a specific order they will display without any gaps but why this should be I have no idea. It's not even anything to do with ID numbers being in numerical order
Hi
I am using default website template what will be my images path in phtml file OR possible can i get http://www.3graces.com theme please
I am using magento 1.6.2.0 my filename sub_category_listing.phtml under /app/design/frontend/base/default/template/catalog/navigation. block identifier sub_category_listing and content {{block type=”catalog/navigation” template=”catalog/navigation/sub_category_listing.phtml”}}.
NOTHING DISPLAY ON HOME PAGE