I want to display main categories on home page like below image,
for that I tried below code in phtml file, but it shows only category name, not display category image,
<?php
$categoryHelper = $this->helper('Magento\Catalog\Helper\Category');
$outputhelper = $this->helper('Magento\Catalog\Helper\Output');
foreach($categoryHelper->getStoreCategories() as $category):
?>
<li>
<?php
$_imgHtml = '';
if ($_imgUrl = $category->getImageUrl()) {
$_imgHtml = '<img src="' . $_imgUrl . '" />';
$_imgHtml = $outputhelper->categoryAttribute($category, $_imgHtml, 'image');
}
?>
<a href="<?php echo $categoryHelper->getCategoryUrl($category) ?>">
<?php echo $_imgHtml; ?><?php echo $category->getName() ?>
</a>
</li>
<?php endforeach; ?>
i have created one phtml file in
Magento_Catalog/templates/product/display_by_cat.phtml
folder
here is my new updated Code..but has also not shown the category image.
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categoryHelper = $this->helper('Magento\Catalog\Helper\Category');
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$cat = $categoryHelper->getStoreCategories()
?>
<ul class="sub-cat-ul">
<?php
foreach ($cat as $cats) {
$_category = $objectManager->create('Magento\Catalog\Model\Category')->load($cats->getId());
$_outputhelper = $this->helper('Magento\Catalog\Helper\Output');
$caturl = $cats->getUrl();
$_imgHtml = '';
if ($_imgUrl = $_category->getImageUrl()) {
$_imgHtml = '<img src="' . $_imgUrl . '" />';
$_imgHtml = $_outputhelper->categoryAttribute($_category, $_imgHtml, 'image');
}
?>
<li>
<?php
if ($_category->getImageUrl()) { ?>
<?php echo '<a class="info no-bg" href="'.$caturl.'" title="'.$cats->getName().'">' .$_imgHtml. '</a>' ?>
<?php } else { ?>
<?php echo '<a class="info" href="'.$caturl.'" title="'.$cats->getName().'">' .$_imgHtml. '</a>' ?>
<?php } ?>
<h4><a href="<?php echo $caturl ?>"><?php echo $cats->getName(); ?></a></h4>
</li>
<?php } ?>
</ul>
It is the Output.. and call this phtml file in cms block. please help me on this.