2

I'm currently loading the child categories of a parent category by ID on the homepage:-

$preparednessCategoryId = 3;
$pcat = Mage::getModel('catalog/category')->load($preparednessCategoryId);
$psubcats = $pcat->getChildren();

foreach(explode(',',$psubcats) as $psubCatid):
$_category = Mage::getModel('catalog/category')->load($psubCatid);

    if($_category->getIsActive()):

        //markup here

    endif;

But I can't for the life of me display the category thumbnail and despite reading 400 odd other posts about the same difficulty, nothing I have tried has worked.

From what I can gather, it needs to be passed into the collection(?) but the following doesn't work for me:-

$_category = Mage::getModel('catalog/category')->load($psubCatid)->getThumbnail();

2 Answers 2

2

Ran into the same problem if you are trying to display category thumbnails on a grid.

Ultimately I found this module the most useful:

Also, refrain from doing while/foreach loops with a Mage::getModel()->load() as it does not scale well.

2
  • Thanks @B00MER, I shall check out that extension and see if we can work around the problem by using parts of it. Good call on pre-fetching outside of the foreach loop, once we know we can actually get the category thumbnail, we'll improve the code.
    – zigojacko
    Commented Mar 10, 2015 at 8:34
  • 1
    As a workaround (solution?), I am now loading getImageURL() but resizing the images to use instead as the category thumbnails. It does the job so that will do for now. Thanks.
    – zigojacko
    Commented Mar 10, 2015 at 9:55
0

I have similar functionality, albeit on the parent category pages to list its child (sub) categories.

Count the number of subcategories:

<div id="category-landing">
    <?php $_numCats = count($this->getCurrentCategory()->getChildrenCategories()); ?>
    <?php foreach ($this->getCurrentCategory()->getChildrenCategories() as $_subcat): ?>

Foreach subcategory, grab the name:

    <?php $_catName = Mage::helper('catalog/output')->categoryAttribute($_subcat, $_subcat->getName(), 'name'); ?>
        <div class="category-landing-row col-md-12 col4-set">

Create the content, with thumbnails:

            <div class="sub-category col-<?php echo $col.' '.$i; if($i%4==0): echo " last"; endif; ?>">
            <?php if ($_thumbnail = Mage::getModel('catalog/category')->load($_subcat->getId())->getThumbnail()): ?>
                <?php $_imageurl = Mage::getBaseUrl('media') . 'catalog/category/' . $_thumbnail;?>
                <div class="sub-category-image">
                    <a title="<?php echo  $_catName ?>" href="<?php echo $_subcat->getUrl() ?>"><img width="155" height="155" alt="<?php echo  $_catName ?>" src="<?php echo $_imageurl ?>"></a>
                </div>
            <?php endif; ?>

            <div class="sub-categories">
                <a title="<?php echo  $_catName ?>" href="<?php echo $_subcat->getUrl() ?>"><?php echo  $_catName ?></a>
                <?php if (count($_subcat->getChildrenCategories())>0): ?>  
                    <ul class="sub-sub-categories">
                    <?php foreach ($_subcat->getChildrenCategories() as $_subsubcat):?>
                        <li><a href="<?php echo $_subsubcat->getUrl() ?>"><?php echo Mage::helper('catalog/output')->categoryAttribute($_subsubcat, $_subsubcat->getName(), 'name') ?></a></li>
                    <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </div>

Close it all out:

        </div>
    </div>
    <?php endforeach ?>
</div>

I've taken out a counter to limit the number of subcat's displayed, but the functionality is there.

The complete code:

<div id="category-landing">
    <?php $_numCats = count($this->getCurrentCategory()->getChildrenCategories()); ?>
    <?php foreach ($this->getCurrentCategory()->getChildrenCategories() as $_subcat): ?>
    <?php $_catName = Mage::helper('catalog/output')->categoryAttribute($_subcat, $_subcat->getName(), 'name'); ?>
        <div class="category-landing-row col-md-12 col4-set">
            <div class="sub-category col-<?php echo $col.' '.$i; if($i%4==0): echo " last"; endif; ?>">
            <?php if ($_thumbnail = Mage::getModel('catalog/category')->load($_subcat->getId())->getThumbnail()): ?>
                <?php $_imageurl = Mage::getBaseUrl('media') . 'catalog/category/' . $_thumbnail;?>
                <div class="sub-category-image">
                    <a title="<?php echo  $_catName ?>" href="<?php echo $_subcat->getUrl() ?>"><img width="155" height="155" alt="<?php echo  $_catName ?>" src="<?php echo $_imageurl ?>"></a>
                </div>
            <?php endif; ?>
            <div class="sub-categories">
                <a title="<?php echo  $_catName ?>" href="<?php echo $_subcat->getUrl() ?>"><?php echo  $_catName ?></a>
                <?php if (count($_subcat->getChildrenCategories())>0): ?>  
                    <ul class="sub-sub-categories">
                    <?php foreach ($_subcat->getChildrenCategories() as $_subsubcat):?>
                        <li><a href="<?php echo $_subsubcat->getUrl() ?>"><?php echo Mage::helper('catalog/output')->categoryAttribute($_subsubcat, $_subsubcat->getName(), 'name') ?></a></li>
                    <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </div>
        </div>
    </div>
    <?php endforeach ?>
</div>
2
  • Are you actually using Magento 1.9.X where you have this working? getThumbnail doesn't exist in the category model, I had to manually add the function (even then, it still doesn't load it). Plus you have to run the SQL queries to create the attribute. None of this you mentioned which makes me think that the version of Magento you are running is < 1.9 when the category thumbnail exists?
    – zigojacko
    Commented Mar 10, 2015 at 8:27
  • @zigojacko You are correct, I'm using Magento 1.8.1.0
    – DomainSoil
    Commented Mar 10, 2015 at 13:34

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.