How to Show/Display Sub-Categories in Magento's static blocks?

Posted by Damodar Bashyal on October 11, 2011

 

It's quite easy to display magento's sub-categories under top level category page. I keep loosing the previous work, so i am writing here so i can find it faster next time. And, You can use it too if you want to :) Any suggestions is welcome.

Add below code on file: catalog/navigation/sub_category_listing.phtml

<?php
/*
 * How to do questions answered for magento @Technooze.com
 * Author: Damodar Bashyal @dbashyal
 * Project:: Magento e-Commerce show sub categories.
 *
 * How to display sub categories instead of products in parent categories?
 */
 
/*
 * Magento:: Get Current Category of the page you are in.
 */
 $category = $this->getCurrentCategory();
 $id = $category->getId();
 ?>

<?php
/*
 * Magento:: Get Child Categories of the current category.
 */
 $_categories = $this->getCurrentChildCategories();
 ?>

<?php
/*
 * Loop through sub - categories
 */
foreach ($_categories as $_category): ?>
    <div class="categorylisting">
    
        <?php
        /*
         * Check to see if the category is active
         */
        if ($_category->getIsActive()): ?>
        
        <?php
        /*
         * Load all magento data for the category selected with category ID.
         */
        $_category = $_category->load($_category->getId()); ?>
        
        <div class="product-image">
            <a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>">
                <img src="<?php echo $this->helper('timage')->init($_category->getImageUrl())->resize(null, 120) ?>" alt="<?php echo $this->htmlEscape($_category->getName()) ?>"/>
            </a>
        </div>
        
        <h2>
            <a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"><?php echo $this->htmlEscape($_category->getName()) ?></a>
        </h2>

        <?php
        /*
         * Display Category Description
         */
        echo $_category->getDescription() ?>
        <?php endif; ?>
    </div>
<?php endforeach; ?> 

Add {{block type="catalog/navigation" template="catalog/navigation/sub_category_listing.phtml"}} on the cms / static blocks wrapped with your design html code.

Then goto Magento's category manager and select the top level category and then click Display Settings tab. Now on display mode select "Static block only" and on CMS Block select "Display Sub categories" [,the name you set when creating static block].

Now, when you go to category page of that top level category, you'll see sub - categories.

Did it help you? add your vote on Like count.

- cheers!

abdulkadir posted on - Monday 20th of February 2012 10:03:56 AM

this is very nice and well worked
 
not published on website


QR Code: How to Show/Display Sub-Categories in Magento's static blocks?