how to display thumbnail from category using getThumbnailUrl() in Magento

后端 未结 3 913
轮回少年
轮回少年 2021-01-24 07:03

I have bee trying to make this work but had no luck, Basically I need to display the main menu categories on the content block and I did, but now I need to display the thumbnail

相关标签:
3条回答
  • 2021-01-24 07:40

    There is no need to change app/code/local/Mage/Catalog/Model/Category.php

    It can be done easily through these line of code...try this...Its Works

    $child= Mage::getSingleton('catalog/layer')->getCurrentCategory()->getId();
    
    $imageSrc = Mage::getModel('catalog/category')->load($child)->getThumbnail();
    
    $ThumbnailUrl = Mage::getBaseUrl('media').'catalog/category/'.$imageSrc;
    
    echo "<img src='{$ThumbnailUrl}' />";
    
    0 讨论(0)
  • 2021-01-24 07:56

    Use this below function to display category thumbnail image

       public function getThumbnailImageUrl() 
       {
          $url = false;
    
          if ($image = $this->getThumbnail()) {
    
             $url = Mage::getBaseUrl('media').'catalog/category/'.$image;
          }
          return $url;
       }
    

    Then, using for any category :

    $_imageUrl=$this->getCurrentCategory()->getThumbnailImageUrl()

    you can get the thumbnail image.

    Refer this article http://www.douglasradburn.co.uk/getting-category-thumbnail-images-with-magento/

    0 讨论(0)
  • 2021-01-24 07:56

    Here's my solution working on Magento 1.7.0.2

    Create sub-categories.phtml file

    Location: app/design/fronend/YOUR-THEME/default/template/catalog/navigation/sub-categories.phtml  
    

    Take note of where to pull thumbnail images from. You will need to add an absolute path of your website at www.yourwebsitenamehere.com below.

    sub-categories.phtml file contents:

    <div id="categories">
      <?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);
               $catName = $this->getCurrentCategory()->getName();
               if($_imageUrl = !$this->getCurrentCategory()->getThumbnailImageUrl()):
                  ?>
                  <?php /* Default subcategory jpg if no image exists */ ?>
                  <div class="category-box">
                     <div class="category-image-box">
                        <a href="<?php echo $this->getCategoryUrl($_category) ?>">
                           <img src="<?php echo $this->getSkinUrl('images/subcategory-default.jpg') ?>">
                        </a>
                     </div>
                     <div>
                        <p>
                           <a href="<?php echo $this->getCategoryUrl($_category) ?>"> <?php echo $catName ?></a>
                        </p>
                     </div>
                  </div>
               <? endif ?>
               <? if($_imageUrl = $this->getCurrentCategory()->getThumbnailImageUrl()): ?>
               <?php /* Displays the subcategory image */ ?>
               <div class="category-box">
                  <div class="category-image-box">
    
                     <a href="<?php echo $this->getCategoryUrl($_category) ?>">
                        <img src="http://www.yourwebsitenamehere.com/media/catalog/category/<?php echo $_imageUrl ?>">
                     </a>
                  </div>
                  <div>
                     <p>
                        <a href="<?php echo $this->getCategoryUrl($_category) ?>"> <?php echo $_category->getName() ?></a>
                     </p>
                  </div>
               </div>
            <? endif; endif; ?>
         <? endforeach ?>
         <?php $layer->setCurrentCategory($_maincategorylisting); ?>
       <? endif; ?>
    </div>
    

    Create a static block.
    1. Block Title: Sub Category Listing
    2. Identifier: sub-categories
    3. Content: {{block type="catalog/navigation" template="catalog/navigation/sub-categories.phtml"}}

    Create Category.php file
    Copy app/code/core/Mage/Catalog/Model/Category.php to app/code/local/Mage/Catalog/Model/Category.php. Once copied, edit file.

    Once inside file, look near line 491. Find:

    public function getImageUrl()
    {
        $url = false;
        if ($image = $this->getImage()) {
            $url = Mage::getBaseUrl('media').'catalog/category/'.$image;
        }
        return $url;
    }
    

    After this paste in:

    /**
     * Retrieve thumbnail image URL
     *
     * @return string
     */
    public function getThumbnailImageUrl($fullpath = false)
    {
    
      $url = false;
    
      if ($image = $this->getThumbnail()) {
    
          if ($fullpath == true) {
              $url = Mage::getBaseUrl('media').'catalog/category/'.$image;
          } else {
              $url = $image;
         }
      }
    
      return $url;
    
    }
    

    Backend Magento.
    1. Select Catalog > Manage Categories.
    2. Create or edit the main category that will display thumbnails from sub categories.
    3. Under Display Settings Tab
    4. Display mode: Static block only
    5. CMS Block: Sub Category Listing
    6. Is Anchor: No
    7. Thumbnail Image: Choose your file

    If you do not see your edits make sure to flush your Magento cache.

    0 讨论(0)
提交回复
热议问题