Pergunta

So there's some code floating around that gets an individual subcategory's image:

foreach ($collection as $cat){
    $cur_category = Mage::getModel('catalog/category')->load($cat->getId());
    $layer = Mage::getSingleton('catalog/layer');   
    $layer->setCurrentCategory($cur_category);
    $_img = $this->getCurrentCategory()->getImageUrl();
}

Where $cat is the subcategory I'm trying to get the image of. The problem I'm running in to - the images for the subcategories are all the same! The first image from the subcategories is the image that shows up on the page. The links and names are all correct though: (this is also in the for loop)

<div>
    <a href="<?php echo $helper->getCategoryUrl($cat);?>">
        <img src="<?php echo $_img; ?>" title="<?php $cat->getName() ?>"/>
        <cite><?php echo $cat->getName();?></cite>
    </a>
</div>

I'm making this modification in catalog/category/view.phtml. There's a bit more to it than shown, but this is all the relevant information.

Edit

All categories have their own unique images, which are properly uploaded. They show in the admin correctly. Also, $cat->getId() is returning the correct id's for the individual subcategories.

Foi útil?

Solução

@RandyHall, are your sure that $this->getCurrentCategory() will get the category from the same place as $layer->setCurrentCategory($cur_category); will set it to?

Watch the source code here and here. As you can see category is set to the layer model and get returns category from registry(via call to block).

So I would suggest you to do something like this:

$_img = $layer->getCurrentCategory()->getImageUrl();

Outras dicas

Perhaps you are doing wrong when getting image url.

instead of using

$_img = $this->getCurrentCategory()->getImageUrl(); 

try below code

$_img = $cur_category->getImageUrl();
foreach ($collection as $cat){
    $cur_category = Mage::getModel('catalog/category')->load($cat->getId());
    $_img = $cur_category->getImageUrl();
}

Looks to me like this is what you are going for?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top