0

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>

2 Answers 2

1

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  ?>" />
2
  • I cant use this in magento 2.1. Mage is deprecated. is there any other way?
    – Joey
    Commented Sep 14, 2017 at 10:17
  • @Joey can u try with below answer Commented Sep 14, 2017 at 12:16
1

For Magento 2 as Mage is deprecated in this

We can try with $category->getImageUrl())

Take a look:

vendor/magento/module-catalog/view/frontend/templates/category/image.phtml

2
  • it doesn't work sometimes when the page we used is not the list page. sometimes it'll give result as expected
    – Joey
    Commented Sep 14, 2017 at 12:22
  • 1
    @Joey will update the answer once i got the solution for the same Commented Sep 14, 2017 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.