I'm trying to get image for category in magento 2
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Model\Layer\Resolver $layerResolver,
public function getCurrentCategory()
{
return $this->_catalogLayer->getCurrentCategory();
}
public function getCurrentChildCategories()
{
$categories = $this->_catalogLayer->getCurrentCategory()->getChildrenCategories();
return $categories;
}
public function getCurrentChildSubCategories($cat)
{
$subcategories = $cat->getChildrenCategories();
return $subcategories;
}
and phtml:
<?php $_categories = $block->getCurrentChildCategories(); ?>
<?php
foreach ($_categories as $category):
echo ("</br>".$category->getName());
$_subcategories = $block->getCurrentChildSubCategories($category);
foreach ($_subcategories as $subcategory):
echo ("</br>_".$subcategory->getName());
endforeach;
endforeach;
?>
but there are no image href column name in category collection. How can I get image in category collection or how can i get image by entity_id in magento 2 correctly? Any suggestions will be helpful.
Best Answer
You should add an event in etc/frontend/events.xml
and the observer: