Tips For Creating Dynamic Category Landing Pages

This is quite a common want, so I thought I would put together a quick tutorial with some ideas and pointers. My main goal will be to give you a starting point for building a static block and PHTML file that can be applied to top level categories to dynamically create a block with of all the subcategories.

The first thing to do is create a new static block (CMS → Static Blocks), lets call it ‘Dynamic Landing Pages’. Within the content paste this:

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

If you’ve not seen one of these before, then basically it loads the list.phtml into the block allowing you to include dynamic content in your site.

Next create list.phtml in app/design/frontend/default/your_theme/template/catalog/category/

Then go to ‘Manage Categories’ select the relevant category and then choose ‘Static Block Only’ in the ‘Display Mode’ dropdown and then choose the ‘Dynamic Landing Pages’ static block we’ve just created from the ‘CMS Block’ dropdown.

Now to look at some coding to put in list.phtml, to get the category id of the current category and an array containing the ids of its child categories, use the following:

?Download list.phtml
1
2
3
4
$current = $this->getCurrentCategory()->getId();
$category = Mage::getModel('catalog/category')->load((int)$current);
$children = $category->getChildren();
$children = explode(",",$children);

This leaves you with:
$current, your current category id.
$category, which is an instance of Mage_Catalog_Model_Category.
$children, is an array of the child category ids.

Then cycle through each of the child categories by loading the category object using the id from the array. We check first that the first value is not just an empty string (as it will be if there are no categories):

?Download list.phtml
5
6
7
8
9
10
11
12
if (strlen($children[0]) > 0)
{
	foreach($children as $child)
	{
		$_child = Mage::getModel('catalog/category')->load($child);
			// then use the $_child object to pull out category properties
	}
}

Some of the key methods that I then subsequently used to create the landing pages were:

  • $_child->getName()
  • $_child->getUrl()
  • $_child->getImageUrl()
  • $_child->getProductCount()

The Image URL is based on the image that is uploaded in the ‘General Information’ tab in ‘Manage Categories’ but be aware that if you use this functionality you will probably want to edit frontend/default/your_theme/template/catalog/category/view.phtml so that it doesn’t load that image at the top of the subcategory.

To view the methods of the Mage_Catalog_Model_Category class look at the file: app/code/core/Mage/Catalog/Model/Category.php.

Comments and questions welcome :)

6 Responses to “Tips For Creating Dynamic Category Landing Pages”

Comments

  1. Kristina  on May 29th, 2009

    I would love to see an example of the final result, as I’m only 40% certain I know what all of this is written to achieve as I read it through. But it’s very comprehensive and, if I’m right about what it does, I can’t wait to try it. Thank you!

  2. Julien B.  on June 16th, 2009

    Really a good tip, works even on home page for the root category ! perfect !!! *****

  3. Paradise  on July 27th, 2009

    Thanks, works greate. The only thing i can’t figure out is how to get the “description” of the subcategories. Im not a php crack so please help me :-)

  4. zzarazza  on July 29th, 2009

    Right what I needed, thanks a lot!

  5. RoKoN BoSS  on August 9th, 2009

    Nice Work…..Thanks

  6. valnech  on December 15th, 2009

    You helped me so much! Thanks

Trackbacks/Pingbacks

Leave a Reply