3

I've built a new Page-Template in Magento. On this template I will show the Category-Image and after that, the product grid (as standard). How can I get the Category-Image for my Template?

From my Template:

<div class="col-main grid4-3 grid-col2-main in-col2">
    <?php echo $this->getChildHtml('global_messages') ?>
    <div class="worlds-image"">Category Image</div>
    <?php echo $this->getChildHtml('content') ?>
</div>
2
  • 1
    Your product can belong to several categories. Which category image you would like to show in this case?
    – user487772
    Jun 26, 2013 at 9:50
  • In Backend, I choose my Category, take a cat.Image and choose several cat.Products and for this page, I've made a new Template (Page layout) and on that template / will show the cat.Products and that Image. Jun 26, 2013 at 11:45

3 Answers 3

8

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  ?>" />
1
  • Mage is not found in magento 2. any fix for that?
    – Joey
    Sep 14, 2017 at 11:02
4

Great, and

<?php $categoryImage = Mage::getModel('catalog/layer')->getCurrentCategory()->getImage(); //Get the file name of the Image stored for the category ?>
                    <img src="<?php echo Mage::getBaseUrl('media').'catalog/category/'.$categoryImage  ?>" />

..is for the category Image. Thanks for your help, It's not easy for a newbie to make "easy things" in magento .

1
  • You're welcome. Glad I could help out.
    – Shatir
    Jun 28, 2013 at 9:13
3

An easier way to grab the full url of the category image (on Magento 1.7, anyway) is to use the function "getImageUrl()". Thus, we can grab more or less the same code from the category view.phtml to create our category image...

<?php 
     $_category = Mage::getModel('catalog/layer')->getCurrentCategory();
     $_catImgHtml = '';
     if ($_catImgUrl = $_category->getImageUrl()) {
         $_catImgHtml = '<p class="category-image"><img src="'.$_catImgUrl.'" alt="'.$this->escapeHtml($_category->getName()).'" title="'.$this->escapeHtml($_category->getName()).'" /></p>';
         $_catImgHtml = $_helper->categoryAttribute($_category, $_catImgHtml, 'image');
     }
?>

You can even simplify this if all you want is a bare link:

$_catImgHtml = '<img src="'.$_catImgUrl.'">';

followed by...

<?php if($_catImgUrl): ?>
     <?php echo $_catImgHtml ?>
<?php endif; ?>
1
  • I had to remove the $_helper line Aug 21, 2015 at 15:07

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.