Ignacio Correia
Ignacio Correia

Reputation: 3668

Magento 1.7 All Categories and respective Thumbnails

I need to list all categories and respective thumbnails. The problem is that I can't display the thumbnails. I have tried all tutorials and nothing. Here is a list of posts explaning different approaches but nether one works: List of examples that I cant make them work

This is my current code:

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>

<?php if (count($_categories) > 0): ?>
    <ul>
        <?php foreach($_categories as $_category): ?>
            <li>
                <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                    <?php echo $_category->getName() ?>
                </a>
                <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
                <?php $_subcategories = $_category->getChildrenCategories() ?>
                <?php if (count($_subcategories) > 0): ?>
                    <ul>
                        <?php foreach($_subcategories as $_subcategory): ?>
                            <li>
                                <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
                                    <?php echo $_subcategory->getName() ?>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

Upvotes: 0

Views: 707

Answers (1)

Ignacio Correia
Ignacio Correia

Reputation: 3668

Got it. Here is the result, we need to the attribute to the collection and then get the image from the respective folder:

<?php $_helper = Mage::helper('catalog/category'); ?>
<?php $_categories = $_helper->getStoreCategories(false, true, false)
                    //Here is the solution
                    ->addAttributeToSelect('thumbnail')                    
                    ->addOrderField('name');
 ?>
<?php if (count($_categories) > 0): ?>
    <ul>
        <?php foreach($_categories as $_category): ?>
            <li>
                <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                    <?php 
                        echo $_category->getName();
                        echo '<img src="'.Mage::getBaseUrl('media').'catalog/category/'.$_category->getThumbnail().'" width="100" height="100"/>';
                    ?>
                </a>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

Upvotes: 1

Related Questions