Magento – subcategory listing in sidebar of category page

categorysidebar

I need to show subcategories in sidebar for currently opened category page.

e.g

CMS
  wordpress
    Art
    Auction
  joomla
  drupal 

if i am on cms page it shows wordpress, joomla ,drupal in sidebar.
if i am on wordpress it shows art and auction.
but if i am on art page , left side is blank..in that case i want it to show
Wordpress (its parent)
and its subcategories.

Please suggest

EDIT

This is the code.

there is hierarchy of category, but for that last categoy in this case 'art'
has no subcategories in it.
So the sidebar comes out to be blank.

<!-- subcategory code -->

<?php

$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
        ->addAttributeToSelect(array('name', 'thumbnail'))
        ->addAttributeToFilter('is_active', 1)
        ->addIdFilter($category->getChildren())
 ?>
 <div class="block">

 <div class="block-title">
    <span><?php echo $category->getName() ?></span>

    </div>
    <div class="block-content clearfix">
<ul class="subcategories">
    <?php foreach ($categories as $category): ?>
        <li>
            <a href="<?php echo $category->getUrl() ?>"><!--<img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $category->getThumbnail() ?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" />-->
                <span><?php echo $category->getName() ?>
               <?php $collection = Mage::getModel('catalog/product')->getCollection()->addCategoryFilter($category);

echo "(".count($collection).")"; ?></span>
                </a>
        </li>
    <?php endforeach; ?>
</ul></div>
</div>
<!-- subcategory code -->

on art category page ,i want if it has no subcategories than show its parent with its sub in sidebar.

Best Answer

You can use the following code at the top :

$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
if(!$category->getChildren()){
 //$parentcat_id = $category->getParentCategory()->getId();
 //$category = Mage::getSingleton('catalog/category')->load($parentcat_id);
 $category = $category->getParentCategory();
}
$categories = $category->getCollection()
        ->addAttributeToSelect(array('name', 'thumbnail'))
        ->addAttributeToFilter('is_active', 1)
        ->addIdFilter($category->getChildren());
Related Topic