3

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.

  • where is artpage lyes ? – Rajeev K Tomy Aug 29 '14 at 6:09
  • can you post the sidebar code, which is used for categories – MTM Aug 29 '14 at 6:16
2

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());
  • happy it worked for you, please accept the answer by clicking the tick so it turns to green. – MTM Aug 30 '14 at 12:33

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.