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 = '<amp-img src="' . $_imgUrl . '" width="600" height="400" class="amp-wp-unknown-size amp-wp-enforced-sizes" sizes="(min-width: 600px) 600px, 100vw"></amp-img>'; $_imgHtml = $_outputhelper->categoryAttribute($category, $_imgHtml, 'image'); echo $_imgHtml; } |