2

Currently these are being rendered with cms blocks. Is there a way to include this type of functionality in a template which gets categories and subcategories and displays them like so:

example image

We just want to ensure that if links change or categories change in the admin panel that the links on the frontend will adjust automatically. What's the most efficient way to achieve this?

  • 2
    This would be really useful to know, thanks for asking as we've just used static blocks before, but when you get into multistores this becomes a real headache. – ylluminate Sep 24 '13 at 23:47
2

simple way is to add a phtml file, which uses the following collection:

$categories = Mage::getSingleton('catalog/category')
->getCollection()
->addAttributeToSelect('name')
->addIsActiveFilter()
->addFieldToFilter('level', array('gt' => '1'));

and a foreach loop

foreach ($categories as $category):
...
your html
...
endforach;

and call that phtml file in your cms page / static block.

EDIT:

More reference:

add categories with images on homepage – magento

category tree in magento with more level on the frontend

Magento - get a parent category and all sub-sub-categories

The effiient way would be, to create a module with just Block and Helper classes

  1. phtml instance to be instantiated in Block.
  2. Helper class to contain a method/function to fetch the children categories of the given category id.
  3. This method can be called recursively in phtml file to display all the categories and their sub categories.

for example:

$children = Mage::getModel('catalog/category')->load(_CATEGORY_ID_)->getChildrenCategories();
            foreach($children as $child){
                echo $child->getName() . " " . $child->getUrl() . "<br/>";
            }
  • 1
    You might want to change the level filter to ->addFieldToFilter('level', array('gt' => '1'));. Level 0 is for "root of roots" and level 1 is for the root categories. You want to exclude them. – Marius Sep 25 '13 at 7:46
  • done editing, I thing they wanna show the root categories as well. – Shathish Sep 25 '13 at 7:48
  • 2
    Root categories are usually not shown anywhere. They are used for setting the category tree to a website. a root category is 'Root Category' in the backend. – Marius Sep 25 '13 at 7:51
  • I'm bit confused now, does that mean "Root Catalog" is level 1? I thought it was level 0! – Shathish Sep 25 '13 at 7:57
  • 3
    Yep. Check the table catalog_category_entity. There is an entry there that is not visible in the frontend with ID 1 and level 0. That's a "root of roots" and all catalog roots are children of that "hidden" category. You can also check in a custom script by doing this: $category = Mage::getModel('catalog/category')->load(ROOT_ID_HERE); var_dump($category->getLevel()). You should get 1. – Marius Sep 25 '13 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.