Show category image programmatically in magetno 2

There are two options on the Magento to show the category image on the description page

In your layout.xml file add below

<block class="Magento\Framework\View\Element\Template" template="Test_Demo::template.phtml">
<arguments>
    <argument name="category_list" xsi:type="object">\Test\Demo\ViewModel\CategoryList</argument>
</arguments>

After creating a new file this file add the ViewModel folder and add the code below.file name is CategoryList.php

 <?PHP
  namespace Test\Demo\ViewModel;
  use Magento\Catalog\Model\Category;
  use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
  use Magento\Framework\Exception\LocalizedException;
  use Magento\Framework\View\Element\Block\ArgumentInterface;

class CategoryList implements ArgumentInterface
{
    /**
     * @var CollectionFactory
     */
    private $categoryFactory;
    /**
     * @var Category
     */
    private $category;

    public function __construct(
        CollectionFactory $categoryFactory,
        Category $category
    ) {
        $this->categoryFactory = $categoryFactory;
        $this->category = $category;
    }

    /**
     * @return array
     */
    public function getList()
    {
        $collection = $this->categoryFactory->create();
        $items = [];
        $collection->addFieldToFilter('is_active', ['eq' => 1]);
        foreach ($collection as $key => $categoryData) {
            /** @var \Magento\Catalog\Model\Category $category */
            try {
                $category = $this->category->load($categoryData->getID());
                $items[$key]['image'] = $category->getImageUrl();
                $items[$key]['name'] = $category->getName();
            } catch (LocalizedException $e) {
            }
        }
        return $items;
    }
}

and your template file add the below code. file name is template.phtml

 <?PHP
     $viewModel = $block->getCategoryList();
     $categories = $viewModel->getList();

 ?>
      <div class="home-cat-wrap">
        <div class="category">
            <?php foreach ($categories as $category): ?>
                <div class="cat-item">
                    <div class="cat-img">
                        <img src="<?= $category['image'] ? $category['image'] : "http://demo.link" ?>" alt="">
                    </div>
                    <h4><a href="#"><?= $category['name'] ?></a></h4>
                </div>
            <?php endforeach; ?>
        </div>
        <div class="view-more"><a class="action primary">View</a></div>
    </div>



2nd option

Using object manager

Magento_Catalog\templates\category\desc_main_column.phtml

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $currentCategory = $objectManager->get(‘Magento\Framework\Registry’)->registry(‘current_category’); // echo $currentCategory->getId(); // echo $currentCategory->getName(); // echo $currentCategory->getDescription(); echo $currentCategory->getImageUrl();

Leave a comment

Your email address will not be published. Required fields are marked *