Home > Magento Tutorials > Adding categories to the homepage

Adding categories to the homepage

Posted on: 18th Feb 2009 By: Adam Moss No Comments

In Magento, you may want to display all categories on the homepage in a similar way to how EKM works. To do it you need to create a new file called list.phtml in this folder: /magento/app/design/frontend/default/default/template/catalog/category/list.phtml

Paste this code into it an upload:

<?php

// Iterate all categories in store
foreach ($this->getStoreCategories() as $_category):

// If category is Active
if($_category->getIsActive()):

// Load the actual category object for this category
$cur_category = Mage::getModel(‘catalog/category’)->load($_category->getId());

// Load a random product from this category
$products = Mage::getResourceModel(‘catalog/product_collection’)->addCategoryFilter($cur_category)->
addAttributeToSelect(‘small_image’);
$products->getSelect()->order(new Zend_Db_Expr(‘RAND()’))->limit(1);

$products->load();

// This a bit of a fudge – there’s only one element in the collection
$_product = null;
foreach ( $products as $_product ) {}
?>

<div class=”categorylisting”>
<div class=”linkimage”><a href=”<?php echo $this->getCategoryUrl($_category)?>”>

<?php
if(isset($_product)):
?>
<img src=”<?php echo $this->helper(‘catalog/image’)->init($_product, ‘small_image’)->resize(135, 135); ?>” width=”135″ height=”135″ alt=”<?php echo $this->htmlEscape($_product->getName()) ?>” />
<?php
endif;
?>
</div>
<h2><?php echo $_category->getName()?></h2></a>
</div>
<?php
endif;
endforeach;
?> 

Then you need to call it on your homepage, so in the CMS area choose the homepage and paste in the following code to show the top-level categories:

{{block type="catalog/navigation" name="catalog.category" template="catalog/category/list.phtml"}}

View the full thread here: http://www.magentocommerce.com/boards/viewthread/14527/P0/

Post A Comment

Your comments:
Enclose code snippets within the appropriate tags: [php][/php]   [js][/js]   [xml][/xml]   [css][/css]   [html][/html]
E.g: [php]<?php echo "hello world"; ?>[/php]

Search Blog

Archives

For the record...

Views & opinions in this blog are those of the individual and do not necessarily reflect those of E-commerce Web Design or the Creare Group.