Magento 2 get category image url


Magento 2 get category image url By Id– We can get category url from category object in Magento 2. Here in this article we are going to explain how you can fetch the url and display the image on phtml.


Magento 2 get category image url By Id Example

Here in the below example we have fetched the category url from category object and displayed it on phtml-

Magento 2 get category image url Example:

 $categoryId = 5;
  $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  $category = $_objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);

  $_outputhelper    = $this->helper('Magento\Catalog\Helper\Output');
  $_imgHtml   = '';
  if ($_imgUrl = $category->getImageUrl()) {

   $_imgHtml = '<img src="' . $_imgUrl . '" />';
   $_imgHtml = $_outputhelper->categoryAttribute($category, $_imgHtml, 'image');
   echo $_imgHtml;

  }

Advertisements

Add Comment

📖 Read More