1

The category image is not visible on the description page.

How can I display it on the page?

app\design\frontend[base][theme]\Magento_Catalog\templates\category\description.phtml

2
  • $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $currentCategory = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category information // echo $currentCategory->getId(); // echo $currentCategory->getName(); // echo $currentCategory->getDescription(); echo $currentCategory->getImageUrl(); Commented Nov 8, 2023 at 12:17
  • I applied it to the other file - Magento_Catalog\templates\category\desc_main_column.phtml Commented Nov 8, 2023 at 17:34

2 Answers 2

2

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>
0

I applied it to the other file

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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