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 subcategories to category pages

Adding subcategories to category pages

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

1. Create a file called subcategory_listing.phtml in app/design/frontend/default/modern/template/catalog/navigation/
And paste in:

<?php $_categories=$this->getCurrentChildCategories(); ?>
<?php foreach ($_categories as $_category): ?>
  <div class="categorylisting">
  <?php if($_category->getIsActive()): ?>
  <div class="catimg">
  <a href="<?php echo $_category->getURL() ?>"
title="<?php echo $this->htmlEscape($_category->getName()) ?>">
  <img src="<?php echo $_category->getImageUrl() ?>" width="90" height="90"
alt="<?php echo $this->htmlEscape($_category->getName()) ?>" />
  </a>
  <?php /* echo "Find this item->" */ ?>
  </div>

  <div class="button"><a href="<?php echo $_category->getURL() ?>"
title="<?php echo $this->htmlEscape($_category->getName()) ?>"><
?php echo $this->htmlEscape($_category->getName()) ?></a></div>

  <?php echo $_category->getDescription() ?>

  <?php endif; ?>
  </div>
  <?php endforeach; ?>

2. Open app\code\core\Mage\Catalog\Block\Navigation.php

Find:

 public function getCurrentChildCategories()
{
$layer = Mage::getSingleton('catalog/layer');
$category   = $layer->getCurrentCategory();
/* @var $category Mage_Catalog_Model_Category */
$categories = $category->getChildrenCategories();
$productCollection = Mage::getResourceModel('catalog/product_collection');
$layer->prepareProductCollection($productCollection);
$productCollection->addCountToCategories($categories);
return $categories;
}

And replace with:

 public function getCurrentChildCategories()
{
$layer = Mage::getSingleton('catalog/layer');
$category   = $layer->getCurrentCategory();
/* @var $category Mage_Catalog_Model_Category */
$collection = Mage::getModel('catalog/category')->getCollection();
/* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
$collection->addAttributeToSelect('url_key')
->addAttributeToSelect('name')
->addAttributeToSelect('is_anchor')
->addAttributeToSelect('image')
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
->setOrder('position', 'ASC')
->joinUrlRewrite()
->load();

$productCollection = Mage::getResourceModel(‘catalog/product_collection’);
$layer->prepareProductCollection($productCollection);
$productCollection->addCountToCategories($collection);
return $collection;
}

3. Create a static block called subcategory_listing and name it Subcategory Listing. Then enter:

 {{block type="catalog/navigation" template="catalog/navigation/subcategory_listing.phtml"}}

4. Create a category, go to Display Settings >

Display Mode: Static Block Only
CMS Block: Subcategory Listing

5. Now if you want to style it in the same way I have, you need the following CSS (edit as appropriate):

 .categorylisting {float:left;width:140px;height:170px;padding:10px;
border:1px solid #ccc;margin:10px;text-align:center;}
.textcat {width:129px;padding:3px;padding-top:5px;height:30px;
border:1px solid #ccc;text-align:center;margin-top:10px;
background-color:#eeeeee;background-image:url(images/headcat.gif);background-repeat:repeat-x;}
.button { margin: 10px auto; font-size:1.2em; font-weight:bold; text-align:center; }
.catimg { margin:0 auto; }

Original thread: http://www.magentocommerce.com/boards/viewthread/14527/P15/

  • 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