Get Current Top Level Category with Magento
How do I get the current (active) top level category and its subcategories??
I do not want the root category, just the highest level category and all of its subcategories.
If I am in the Women’s Category for instance:
Women
- Apparel
-- Shirts
-开发者_如何转开发- Pants
- Accessories
-- Handbags
-- Jewelry
Even if i am looking at shirts, i would like the category tree to remain the same.
Any help would be greatly appreciated.
To give the precise answer to your precise question "how to get the current category and its subcategories" :
To retrieve the current category :
$_currentCategory = $this->getCurrentCategory();
To retrieve its subcategories :
$_categories = $this->getCurrentChildCategories();
The above are working in a catalog/navigation block.
Now, to get the rendering you are speaking about, I think that a simple navigation block with a good use of CSS would do the trick.
Create a navigation block, let's say in your left column :
Create the template file in your template directory structure. In our example : /template/catalog/navigation/thetemplate.phtml
Use this code to draw the whole categories/subcategories structure without the hassle of modifying the code (see [1] at the end of the post....)
Inspect the generated code/CSS and you will see that there are all necessary CSS pointers (levelX, active...) allowing you to display or hide pieces of the categories tree and thus showing only the parts you like.
Conclusion: CSS is sufficient to do what you want to do :)
[1] Code :
<?php $_menu = ''?>
<?php foreach ($this->getStoreCategories() as $_category): ?>
<?php $_menu .= $this->drawItem($_category) ?>
<?php endforeach ?>
<?php if ($_menu): ?>
<div class="THECSS-CONTAINER">
<ul id="THECSS">
<?php echo $_menu; ?>
</ul>
</div>
<?php endif; ?>
精彩评论