1. Home
  2. Prices
  3. Guided Tour
  4. New Shop
  5. Shop Review
  6. Ecommerce Marketing
  7. Ecommerce TV
  8. Magento Developers
  9. Portfolio
  10. Blog
  11. Contact
Home > Magento Tutorials > Adding categories to the homepage

Adding categories to the homepage

Posted on: 18th Feb 2009 By: Adam Moss Leave a comment 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/

  • Twitter
  • AIM
  • WordPress
  • LinkedIn
  • Facebook
  • StumbleUpon
  • Technorati Favorites
  • Delicious
  • Ping
  • Digg
  • Bebo
  • Reddit
  • MySpace
  • NewsVine
  • Google Bookmarks
  • Sphinn
  • Propeller
Categories: Magento Tutorials Tags:

Post A Comment