Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 4
how to get catagory image url
 
glaDiator
Member
 
Avatar
Total Posts:  65
Joined:  2008-03-17
 

I have seen in catalog/category/view.phtml the category image url is retrieved by

$this->getCurrentCategory()->getImageUrl()

somewhere the category is set as current category. I have wasted several hours in finding where & how it is done downer  but unable to find out.
can anyone tell how it is done ?

What i need is to show all the category images in one page. Well i was able to get the category names & category urls as it is done in the TOP vertical category menu.

I also tried to get the category image url in the following way.............

Mage::getModel('catalog/category')->setData($_category->getData())->getImageUrl()

but it shows no result

while following works correctly.............

Mage::getModel('catalog/category')->setData($_category->getData())->getCategoryUrl()
 
Magento Community Magento Community
Magento Community
Magento Community
 
MartinaL
Sr. Member
 
Total Posts:  82
Joined:  2008-03-31
 

I to really need to get this working if anyone could help?

 
Magento Community Magento Community
Magento Community
Magento Community
 
glaDiator
Member
 
Avatar
Total Posts:  65
Joined:  2008-03-17
 

@MartinaL .................. No body seems to answer here ............. after digging up my head for a long time i have come out with a solution (i think it is the best)......... here i paste my whole phtml file ....... you should create a .phtml file in the YOUR_THEME/template/catalog/navigation folder & paste the following code.........

<?php 
$_main_categories
=$this->getStoreCategories();
if(
$_main_categories->count()):
    foreach (
$_main_categories as $_main_category):
        if(
$_main_category->getIsActive()):
            
$_sub_categories=$_main_category->getChildren();
            foreach (
$_sub_categories as $_sub_category):
                if(
$_sub_category->getIsActive()): 
?>
                    <?php
                    $cur_category
=Mage::getModel('catalog/category')->load($_sub_category->getId());
                    
$layer Mage::getSingleton('catalog/layer');
                    
$layer->setCurrentCategory($cur_category);                    ?>                

                    
<div class="catagory-box">
                        <
div class="greenBorder">
                            <
div class="img-align">
                                <
div class="img-valign" align="center">
                                    <
a href="<?php echo $this->getCurrentCategory()->getUrl()?>">
                                    
<?php if($_imageUrl=$this->getCurrentCategory()->getImageUrl()):?>
                                        
<img src="<?php echo $_imageUrl?>alt="<?php echo $this->getCurrentCategory()->getName();?>">
                                    
<?php else:?>
                                        
<img src="<?php echo $this->getSkinUrl('images/add-01.gif')?>" alt="<?php echo $this->getCurrentCategory()->getName();?>">
                                    
<?php endif;?>
                                    
</a>
                                </
div>
                            </
div>
                        </
div>
                        <
div class="cat-box-text">
                            <
span><a href="<?php echo $this->getCurrentCategory()->getUrl()?>"><?php echo $this->getCurrentCategory()->getName();?></a></span><br>
                            
<?php echo $this->getCurrentCategory()->getDescription() ?>
                        
</div>
                    </
div>
<?php
                
endif;
            endforeach;
        endif;
    endforeach;
endif; 
?>

The following lines in the code is the most important codes .......... these codes sets the category as the current category .

<?php
$cur_category
=Mage::getModel('catalog/category')->load($_sub_category->getId());
$layer Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
?>

Let me know if it works ( I have tested it in the latest version )...............

 
Magento Community Magento Community
Magento Community
Magento Community
 
MartinaL
Sr. Member
 
Total Posts:  82
Joined:  2008-03-31
 

Nope, didn’t work.

This is what I have in my browse.phtml page;

<?php
$iCol 
1;
$categories_per_row 3;
$cellwidth intval100 $categories_per_row );
?>
<table width="100%" cellspacing="0" cellpadding="0">
<?php foreach ($this->getStoreCategories() as $_category
    
if ($iCol == 1// this is an indicator wether a row needs to be opened or not
        
echo "<tr>\n";
    
}
    ?>
    
<td align="center" width="<?php echo $cellwidth ?>%" >
        <
br/>
        
<?php echo $this->drawItemwithImage($_category?>
        
<br/>
    </
td>
    
<?php
    
// Do we need to close the current row now?
    
if ($iCol == $categories_per_row// If the number of products per row has been reached
        
echo "</tr>\n";
        
$iCol 1;
    
}
    
else {
        $iCol
++;
    
}
}
// Do we need a final closing row tag?
if (sizeof$_category ) < $categories_per_row{
    
echo "</tr>\n";
}
?>
</table>

And I have added this to the navigation.phtml file;

public function getImageUrl($category)
    
{
        
if ($category instanceof Mage_Catalog_Model_Category{
            $url 
$category->getImageUrl();
        
else {
            $url 
Mage::getModel('catalog/category')
                ->
setData($category->getData())
                ->
getImageUrl();
        
}
        
return $url;
    
}

/**
     * Enter description here...
     *
     * @param Mage_Catalog_Model_Category $category
     * @param int $level
     * @param boolean $last
     * @return string
     */
    
public function drawItemwithImage($category$level=0$last=false)
    
{
        $html 
'';
        if (!
$category->getIsActive()) {
            
return $html;
        
}

        $children 
$category->getChildren();
        
$hasChildren $children && $children->count();
        
$html.= '<li';
        
        
$html.= ' class="level'.$level;
        if (
$this->isCategoryActive($category)) {
            $html
.= ' active';
        
}
        
if ($last{
            $html 
.= ' last';
        
}
        
if ($hasChildren{
            $cnt 
0;
            foreach (
$children as $child{
                
if ($child->getIsActive()) {
                    $cnt
++;
                
}
            }
            $html 
.= ' parent';
        
}
        $html
.= '">'."\n";
        
$html.= '<a href="'.$this->getCategoryUrl($category).'">'
        
$html.='<img src="'.$this->getImageUrl($category).'" border="0" width="150" height="113">';
        
$html.='<br/><br/>'."\n";
        
$html.='<span>'.$category->getName().'</span></a>'."\n";
        
$html.= '</li>'."\n";
        return 
$html;    
    
}

But this doesn’t seem to work either, when I look at the source code there is no image src at all

 
Magento Community Magento Community
Magento Community
Magento Community
 
glaDiator
Member
 
Avatar
Total Posts:  65
Joined:  2008-03-17
 

replace your following function

public function getImageUrl($category)
    
{
        
if ($category instanceof Mage_Catalog_Model_Category{
            $url 
$category->getImageUrl();
        
else {
            $url 
Mage::getModel('catalog/category')
                ->
setData($category->getData())
                ->
getImageUrl();
        
}
        
return $url;
    
}

with this one.......

public function getImageUrl($category)
    
{
    $cur_category
=Mage::getModel('catalog/category')->load($category->getId());
    
$layer Mage::getSingleton('catalog/layer');
    
$layer->setCurrentCategory($cur_category);

            
$url $this->getCurrentCategory()->getImageUrl();
        return 
$url;
    
}

i have not tested the code ......... but i think it should work........

The Idea is ...  u have to set the category as the current Category .... Then u can get the image url by calling the following function

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

Let me know how it goes.............

 
Magento Community Magento Community
Magento Community
Magento Community
 
Paradise
Member
 
Total Posts:  33
Joined:  2007-09-21
Darmstadt
 

I want the categories to show up like in my old shop.
See attachment…
Is that what you talk about here?

Image Attachments
cats.jpg
 
Magento Community Magento Community
Magento Community
Magento Community
 
MartinaL
Sr. Member
 
Total Posts:  82
Joined:  2008-03-31
 

Yes this is what I am trying to achieve, and have changed a few pages to try and do this but can’t seem to get the category images to display next to the name and description!

 
Magento Community Magento Community
Magento Community
Magento Community
 
MartinaL
Sr. Member
 
Total Posts:  82
Joined:  2008-03-31
 

glaDiator ...... worked perfectly! Thanks grin

 
Magento Community Magento Community
Magento Community
Magento Community
 
Paradise
Member
 
Total Posts:  33
Joined:  2007-09-21
Darmstadt
 

Sorry, but can you tell me the whole way how to do it please.

This Magento template drives me grazy :-(

 
Magento Community Magento Community
Magento Community
Magento Community
 
corbykissler
Member
 
Total Posts:  39
Joined:  2008-05-14
 

Great thread - code came in handy -

One thing that should be mentioned that I ran into while using this.

The site I’m working on displays all the subcategories in a central part of the screen - and then also displays them on a sidebar.

Well, the code as it exists - sets the current category to the last subcategory that is a child of the current category.

Then what happens if you have code elsewhere on the page that picks up the current category - it will erroneously pick up the last subcategory of the current category.

So basically, you have to reset the current category to the category you want to reference in the rest of the page.  I set a variable at the top of the code, then called the set category code again, passing along the variable from the top.

LIke so:

<div id="categories">
<
div class="col_full">

<
h2>Product Categories</h2>
        
<
div class="listing">
<?php $_maincategorylisting=$this->getCurrentCategory()?>                        
<?php $_categories
=$this->getCurrentChildCategories()?>
<?php 
if($_categories->count()):?>
           <? 
foreach ($_categories as $_category):?>
                      
                        <? 
if($_category->getIsActive()):
                        
                        
$cur_category=Mage::getModel('catalog/category')->load($_category->getId());
           
$layer Mage::getSingleton('catalog/layer');
           
$layer->setCurrentCategory($cur_category);  
           if(
$_imageUrl=$this->getCurrentCategory()->getImageUrl()):?>
            
                        
<div style="height: 150px; text-align: bottom;">
<
a href="<?php echo $this->getCategoryUrl($_category)?>"><img src="<?php echo $_imageUrl?>"></a><p><a href="<?php echo $this->getCategoryUrl($_category)?>"<?php echo $_category->getName()?></a></p></div>
<?                                              
                        
endif;
                        endif;
?>
            <?endforeach?>
            
            <?php 
/* This resets the category back to the original pages category
****     If this is not done, subsequent calls on the same page will use the last category
****    in the foreach loop
*/   
?>
<?php $layer
->setCurrentCategory($_maincategorylisting);  ?> 
    <?endif
;?>
  
</div>
</
div>
</
div>

Thanks and hope this helps someone else.

 
Magento Community Magento Community
Magento Community
Magento Community
 
blindspott
Jr. Member
 
Total Posts:  1
Joined:  2008-05-15
New Zealand
 

Hi,

I used pretty much glaDiator’s code (shown below) and changed it a lil to fit me:

<?php 
$_main_categories
=$this->getStoreCategories();
if(
$_main_categories->count()):
    foreach (
$_main_categories as $_main_category):
        if(
$_main_category->getIsActive()):
            
$_sub_categories=$_main_category->getChildren();
            foreach (
$_sub_categories as $_sub_category):
                if(
$_sub_category->getIsActive()): 
?>
                    <?php
                    $cur_category
=Mage::getModel('catalog/category')->load($_sub_category->getId());
                    
$layer Mage::getSingleton('catalog/layer');
                    
$layer->setCurrentCategory($cur_category);                    ?>                

                    
<div class="catagory-box">
                        <
div class="greenBorder">
                            <
div class="img-align">
                                <
div class="img-valign" align="center">
                                    <
a href="<?php echo $this->getCurrentCategory()->getUrl()?>">
                                    
<?php if($_imageUrl=$this->getCurrentCategory()->getImageUrl()):?>
                                        
<img src="<?php echo $_imageUrl?>alt="<?php echo $this->getCurrentCategory()->getName();?>">
                                    
<?php else:?>
                                        
<img src="<?php echo $this->getSkinUrl('images/add-01.gif')?>" alt="<?php echo $this->getCurrentCategory()->getName();?>">
                                    
<?php endif;?>
                                    
</a>
                                </
div>
                            </
div>
                        </
div>
                        <
div class="cat-box-text">
                            <
span><a href="<?php echo $this->getCurrentCategory()->getUrl()?>"><?php echo $this->getCurrentCategory()->getName();?></a></span><br>
                            
<?php echo $this->getCurrentCategory()->getDescription() ?>
                        
</div>
                    </
div>
<?php
                
endif;
            endforeach;
        endif;
    endforeach;
endif; 
?>

And this is my code:

<?php 
$_main_categories
=$this->getStoreCategories();
if(
$_main_categories->count()):
    foreach (
$_main_categories as $_main_category):
        if(
$_main_category->getIsActive()):
            
$_sub_categories=$_main_category->getChildren();
            foreach (
$_sub_categories as $_sub_category):
                if(
$_sub_category->getIsActive()): 
                    
$cur_category=Mage::getModel('catalog/category')->load($_sub_category->getId());
                    
$layer Mage::getSingleton('catalog/layer');
                    
$layer->setCurrentCategory($cur_category);                    
?>                
    
    
<div class="img-valign">
        <
a href="<?php echo $this->getCurrentCategory()->getUrl()?>">
        
<?php if($_imageUrl=$this->getCurrentCategory()->getImageUrl()):?>
            
<img src="<?php echo $_imageUrl?>width "110" height "110" alt="<?php echo $this->getCurrentCategory()->getName();?>">
        
<?php else:?>
            
<img src="<?php echo $this->getSkinUrl('images/no-cat-pic.jpg')?>" width "110" height "110" alt="<?php echo $this->getCurrentCategory()->getName();?>">
        
<?php endif;?>
        
</a>
    </
div>
    
<?php
                
endif;
            endforeach;
        endif;
    endforeach;
endif; 
?>

It appears the way i want it to, but for some reason its not getting the current category selected and its displaying the same on all category pages. can someone pleeeease help?

Cheers

 
Magento Community Magento Community
Magento Community
Magento Community
 
corbykissler
Member
 
Total Posts:  39
Joined:  2008-05-14
 

Had a request for this code through email - sharing it with the group . .

Here is how I implemented a subcategory page that shows images and links to sub categories after the users clicks on a main category.

First off , in the catalog.xml file - I put the following reference in the “layers” section - so it’ll look like this:

<catalog_category_layered>
    
    
    
          <
reference name="root">
            <
action method="setTemplate"><template>page/SubPage_2Column.phtml</template></action>
        </
reference>

        <
reference name="subpage_left"
                
            <
block type="catalog/navigation" name="category.subnav" template="catalog/navigation/subcats.phtml" />         
                                
         </
reference>

</
catalog_category_layered>

You’ll have to change your set template value (if you are changing the template) and also the reference name.  In my template, subpage_left is the main content area.

Then, I created a subcats.phtml file and put it in the directory

/template/catalog/navigation subdirectory under my theme - you can put this in the default theme pack if you wish.

Here’s the code for subcats.phtml . . .

Some notes - the first little bit up at the top just prints out the current category name and description before printing out the subcategories.

in pseudo code here is what it does:

Print out the module header (Current Category Name and Description)
Get the Current Category and Save For Later
Get the Current Child Categories
For each child category
- Set the current category to be the child category
-Get the child category link, image, and name
-Print out child category link,image and name
Go to next child category until done

Reset the Current Category to the original current category - saved at top

If no child categories - display No matching products message

done

<h2><?php if($_currentCatName=$this->htmlEscape($this->getCurrentCategory()->getName())): ?><?php echo $_currentCatName ?></h2>
<?php if($_description=$this->getCurrentCategory()->getDescription()): ?>

                
<p><?php echo $_description ?>
                
</p>
                
<?php endif; ?>
                <?php 
endif; ?>

<div id="categories">
<
div class="col_full">

<
h2>Product Categories</h2>
        
<
div class="catlisting">
<?php $_maincategorylisting=$this->getCurrentCategory()?>                        
<?php $_categories
=$this->getCurrentChildCategories()?>
<?php 
if($_categories->count()):?>
           <? 
foreach ($_categories as $_category):?>
                      
                        <? 
if($_category->getIsActive()):
                        
                        
$cur_category=Mage::getModel('catalog/category')->load($_category->getId());
           
$layer Mage::getSingleton('catalog/layer');
           
$layer->setCurrentCategory($cur_category);  
           if(
$_imageUrl=$this->getCurrentCategory()->getImageUrl()):?>
            
                        
<div><div class="linkimage"><p>
<
a href="<?php echo $this->getCategoryUrl($_category)?>"><img src="<?php echo $_imageUrl?>"></a></p></div><p><a href="<?php echo $this->getCategoryUrl($_category)?>"><?php echo $_category->getName()?></a></p></div>
<?                                              
                 
                 
                 
else: ?>
                 
                 
<div><p><a href="<?php echo $this->getCategoryUrl($_category)?>"><?php echo $_category->getName()?></a></p></div>


<?
                 
                 
                 
                        
endif;
                        
                        
                        
                        
                        endif;
?>
            <?endforeach?>
            
            <?php 
/* This resets the category back to the original pages category
****     If this is not done, subsequent calls on the same page will use the last category
****    in the foreach loop
*/   
?>
<?php $layer
->setCurrentCategory($_maincategorylisting);  ?> 

</div>
</
div>
</
div>


<?php else: ?>

</div>
<
div class="note-msg">
    
<?php echo $this->__('There are no products matching the selection.'?>
</div>

</
div>
</
div>
    
<?endif;?>
 
Magento Community Magento Community
Magento Community
Magento Community
 
vesvello
Sr. Member
 
Total Posts:  97
Joined:  2007-12-01
 
corbykissler - 26 June 2008 08:26 AM


You’ll have to change your set template value (if you are changing the template) and also the reference name.  In my template, subpage_left is the main content area.

What does it mean? What do I need to change? where?

 
Magento Community Magento Community
Magento Community
Magento Community
 
batkins
Jr. Member
 
Avatar
Total Posts:  26
Joined:  2008-05-10
 

Thanks so much Corby… I figured out what I was doing wrong. after I created my template, I set it in the custom design tab under categories and included this custom design block:

<reference name="content">
<
block type="catalog/navigation" name="category.subnav" template="catalog/navigation/subcats.phtml" />         
</
reference>
 
Magento Community Magento Community
Magento Community
Magento Community
 
vesvello
Sr. Member
 
Total Posts:  97
Joined:  2007-12-01
 

Another question:

What do I need to do to skip “category list” if there is not a subcategory? I want something like if subcategory doesnt exit, then skip

 
Magento Community Magento Community
Magento Community
Magento Community
 
vesvello
Sr. Member
 
Total Posts:  97
Joined:  2007-12-01
 
vesvello - 04 July 2008 06:56 AM

Another question:

What do I need to do to skip “category list” if there is not a subcategory? I want something like if subcategory doesnt exit, then skip

this is what I did to skip category list if there is not a subcategory:

<?php if($_currentCatName=$this->htmlEscape($this->getCurrentCategory()->getName())): ?>
<?php 
if($_description=$this->getCurrentCategory()->getDescription()): ?>
                <?php 
endif; ?>
                <?php 
endif; ?>
<div id="categories">
<
div class="col_full">
<?php $_maincategorylisting=$this->getCurrentCategory()?>                        
<?php $_categories
=$this->getCurrentChildCategories()?>
<?php 
if(!$_categories->count()):?>
<div class="padder">
    
<?php echo""?>
</div>
</
div>
</
div>
<?php else: ?>
<h2><?php echo $_currentCatName ?></h2>
<
p><?php echo $_description ?>
 
</p>
<
h2>Product Categories</h2>
<
div class="catlisting">
           
<? foreach ($_categories as $_category):?>
                        <? 
if($_category->getIsActive()):
                        
$cur_category=Mage::getModel('catalog/category')->load($_category->getId());
           
$layer Mage::getSingleton('catalog/layer');
           
$layer->setCurrentCategory($cur_category);  
           if(
$_imageUrl=$this->getCurrentCategory()->getImageUrl()):?>
<div><div class="linkimage"><p>
<
a href="<?php echo $this->getCategoryUrl($_category)?>"><img src="<?php echo $_imageUrl?>"></a></p></div><p><a href="<?php echo $this->getCategoryUrl($_category)?>"><?php echo $_category->getName()?></a></p></div>
<?                                              
                 
else: ?>
                 
<div><p><a href="<?php echo $this->getCategoryUrl($_category)?>"><?php echo $_category->getName()?></a></p></div>
<?
                        
endif;
                        endif;
?>
            <?php 
endforeach ?>
            <?php 
/* This resets the category back to the original pages category
****     If this is not done, subsequent calls on the same page will use the last category
****    in the foreach loop
*/   
?>
<?php $layer
->setCurrentCategory($_maincategorylisting);  ?> 
</div>
</
div>
</
div>
    
<?php endif; ?>
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 4