Magento – Magento 2: How to get category image

categoryfrontendmagento-2.1

I want to display the category image of the category.I have a phtml file and is called in cms home page.

<?php 
$subcats = array(99,189);
$imageBlock = $block->getLayout()->createBlock('Magento\Catalog\Block\Category\ListProduct');

foreach($subcats as $cat_id){
    //echo $cat_id;
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $category = $objectManager->create('Magento\Catalog\Model\Category')->load($cat_id);
    $categoryImage = $imageBlock->getImage($category, 'category_page_list');
    echo 'dfdf'.$category->getName();
    //$productImage = $imageBlock->getImage($category);
    echo 'ssds'.$categoryImage;

}

But it is not displaying the image and getting an error.

Please help.

Best Answer

I have Used This code to get Category images in my code Please look at these

<?php
    $category = $this->_objectManager->create('Magento\Catalog\Model\Category')->load($item->getId());
    $_outputhelper    = $this->helper('Magento\Catalog\Helper\Output');
    $_imgHtml   = '';
    if ($_imgUrl = $category->getImageUrl()) {
        $_imgHtml = '<img src="' . $_imgUrl . '" />';
        $_imgHtml = $_outputhelper->categoryAttribute($category, $_imgHtml, 'image');
        /* @escapeNotVerified */ echo $_imgHtml;
    }
?>
Related Topic