lolalola
lolalola

Reputation: 3823

magento show subcategory

HI, Now i get all categorys and subcategory. How get only subcategory?

<?php foreach ($this->getStoreCategories() as $_category): ?>
    <?php $_menu .= $this->drawItem($_category) ?>
<?php endforeach ?>

Upvotes: 2

Views: 3570

Answers (2)

Herv&#233; Gu&#233;tin
Herv&#233; Gu&#233;tin

Reputation: 4392

This will give you "mother" categories + 1st level children categories.

<ul>
 <?php foreach ($this->getStoreCategories() as $_category): ?>
 <li>
  <strong><?php echo $_category->getName(); ?></strong>
   <?php $_children = $_category->getChildren(); ?>
   <?php if($_children->count()) : ?>
  <ul>
     <?php foreach($_children as $_child) : ?>
   <li><?php echo $_child->getName(); ?></li>
     <?php endforeach; ?>
  </ul>
   <?php endif; ?>
 </li>
 <?php endforeach ?>
</ul>

If you only want to display 1st level subcategories just comment/remove the first

<strong><?php echo $_category->getName(); ?></strong>

Upvotes: 5

greg0ire
greg0ire

Reputation: 23265

$this->getStoreCategories() returns a Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection, which has an addPathFilter() method. Look at this method's prototype : it has one argument : $regexp If you know about regular expressions, you can build a regexp which filters your categories by path. Try first to print the categories paths (by doing echo $_category->getPathInStore()), then find a regexp which filters category paths the way you want.

Upvotes: 0

Related Questions