First 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:
$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):
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 subsequently used to create the landing pages were:
$_child->getName()
$_child->getUrl()
$_child->getImageUrl()
$_child->getProductCount()
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 lookat the file:
app/code/core/Mage/Catalog/Model/Category.php
.
by doing this we still not have a subcategories in sort order by its position
ReplyDeleteThis is awesome post regarding create dynamic category in magento.thanks for sharing with this post.I very thankful to you .
ReplyDelete