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

$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.

  • Maybe you should add the text of the error you get? – Petar Dzhambazov Mar 28 '17 at 8:40

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

    $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;
  • yup absolutely.it is working code @aravind – Learing_Coder Jan 23 '18 at 5:28
  • @PranayK How can i get products images using sku? – Gem May 14 '18 at 8:37
  • @Learing_Coder, I'm struggling in getting the category image, Can you please help me? I have tried your code in phtml file but it's not working as expected. – Siva Oct 3 at 6:43

You can use this code. just pass the category id fetch the image

use Magento\Catalog\Model\CategoryRepository;

public function getCategoryImage($categoryId)
    $categoryIdElements = explode('-', $categoryId);
    $category           = $this->categoryRepository->get(end($categoryIdElements));
    $categoryImage       = $category->getImageUrl();

    return $categoryImage;

To get Category Image url:

<?php echo $category->getImageUrl(); ?>

And use it in <img> tag like

<img src="<?php  echo $category->getImageUrl(); ?>" />

And get Alternate Image url:

echo $category->getImageUrl('category_alternate_image'); 
<img src="<?php  echo $category->getImageUrl('category_alternate_image'); ?>" />

I think you are missing here to include helper, you need to use Magento\Catalog\Helper\Output helper. See this link for help. Click here and here to know. How to Create and Use Helpers in Magento 2?


I tried following so that it does not affect performance and you need to call the category model seprately

Open in root of magento 2.2


On line750ish



or replace function

     * Return child categories
     * @param \Magento\Catalog\Model\Category $category
     * @return \Magento\Catalog\Model\ResourceModel\Category\Collection
    public function getChildrenCategories($category)
        $collection = $category->getCollection();
        /* @var $collection \Magento\Catalog\Model\ResourceModel\Category\Collection */

        return $collection;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.