Magento 1.9 – How to Get Image from Current Category

categoryimagemagento-1.9

It's a really simpel question but I'm struggling to long on this one so I'm dropping the question here hoping someone could help me…

I want to get all the subcategories from a parent categorie (this one I got, I can choose the categorie id and I get all the subcategories). But I want to retrieve also the images from those subcategories. I have this in the .phtml file which is loaded on the homepage. So far I got this piece of code:

            <?php
            $category_childs = Mage::getModel('catalog/category')->getCategories(3);
        ?>

        <?php if($_productCollection->count()): ?>
        <div class="product-slider-container">

            <div class="title-container">
                <h2><?php echo $this->__($this->getBlockTitle());?></h2>
                <a href="#" class="jcarousel-prev-horizontal" id="shopper_carousel_prev<?php echo $time; ?>"></a>
                <a href="#" class="jcarousel-next-horizontal" id="shopper_carousel_next<?php echo $time; ?>"></a>
            </div>

            <ul id="featured_<?php echo $time; ?>" class="products-grid jcarousel-skin-tango clearfix">
                <?php foreach ($category_childs as $_category): ?>

                <li class="item">
                    <div class="regular">

                        <a href="<?php echo $_category->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>" class="product-image">
                            <?php echo $this->helper('shoppersettings')->getLabel($_category);  ?>
                            <img id="product-collection-image-" src="<?php echo $this->helper('catalog/image')->init($_category, 'small_image')->resize($imgX, $imgY) ?>" data-srcX2="<?php echo $this->helper('catalog/image')->init($_category, 'small_image')->resize($imgX*2, $imgY*2) ?>" width="<?php echo $imgX; ?>" height="<?php echo $imgY; ?>" alt="<?php echo $this->htmlEscape($_category->getName()) ?>" />
                        </a>
                        <div class="product-info">
                            <?php $_categoryNameStripped = $this->stripTags($_category->getName(), null, true); ?>
                            <h2 class="product-name"><a href="<?php echo $_category->getProductUrl() ?>" title="<?php echo $_categoryNameStripped; ?>"><?php echo $_helper->productAttribute($_category, $_category->getName() , 'name'); ?></a></h2>
                        </div>
                    </div>
                </li>
                <?php endforeach; ?>
            </ul>
            <div class="clear"></div>
        </div>

Best Answer

If you want to get the thumbnail image of the current category that you are browsing, then use the following code:

<?php $categoryImage = Mage::getModel('catalog/layer')->getCurrentCategory()->getThumbnail(); //Get the file name of the Image stored for the category ?>

<img src="<?php echo Mage::getBaseUrl('media').'catalog/category/'.$categoryImage  ?>" />
Related Topic