0

i want to get categories with image at home page, for this i use below code, but i get only category name, its not display category image.

<?php 
  $categoryHelper = $this->helper('Magento\Catalog\Helper\Category');
  foreach($categoryHelper->getStoreCategories() as $category):     
?>
<li>
    <a href="<?php echo $categoryHelper->getCategoryUrl($category) ?>">
        <img src="<?php echo $category->getImageUrl() ?>"><?php echo $category->getName() ?>
    </a>
</li>
<?php endforeach; ?>

please help me on this, how to get category image in this code?

4 Answers 4

0

Try with below code

<?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; ?>
4
  • i use the above code, but it displays the same result as I code already.
    – rocky9310
    Apr 13, 2021 at 5:25
  • Is it returning anything in $category->getImageUrl() ? Apr 13, 2021 at 5:49
  • nimb.ws/6LcW0e pls see this link
    – rocky9310
    Apr 13, 2021 at 6:01
  • There is no category image url attribute in the category object exists, so you need to again load the category using id, you can achieve this using viewmodel. Apr 13, 2021 at 7:21
0

try it like this

$_helper    = $this->helper('Magento\Catalog\Helper\Output');
$_category  = $block->getCurrentCategory();
$_imgHtml   = '';
if ($_imgUrl = $_category->getImageUrl()) {
    $_imgHtml = '<div class="category-image"><img src="' . $_imgUrl . '" alt="' . $block->escapeHtml($_category->getName()) . '" title="' . $block->escapeHtml($_category->getName()) . '" class="image" /></div>';
    $_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
    /* @escapeNotVerified */ echo $_imgHtml;
}
0

Haven't tested this, but it's worth a try:

$categories = $categoryHelper->getStoreCategories(true, true, false);
$categories->addAttributeToSelect(['name', 'image', 'any other attributes you need']);
foreach($categories as $category):
....//your display code here
endforeach;
1
  • I've tried above code but image_url is not getting added to collection in any way. Apr 13, 2021 at 7:22
0

Try this code

app\code\VendoreName\ModuleName\Block

Categoryimage.php

<?php
/**
 * @category Msquare
 * @package Msquare_CategoryImageSlider
 * @author Mahesh Makwana <[email protected]>
 */

namespace VendoreName\ModuleName\Block;

class Categoryimage extends \Magento\Framework\View\Element\Template
{
    protected $_categoryFactory;

    protected $_storeManager;

    protected $_categoryNameFactory;

    protected $categoryImage;

    protected $fileInfo;

    protected $imageHelperFactory;

    protected $assetRepository;

    protected $categoryRepository;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Model\CategoryFactory $categoryNameFactory,
        \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $collecionFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Registry $registry,
        \Magento\Catalog\Model\Category\Image $categoryImage,
        \Magento\Catalog\Model\Category\FileInfo $fileInfo,
        \Magento\Catalog\Helper\ImageFactory $imageHelperFactory,
        \Magento\Framework\View\Asset\Repository $assetRepository,
        array $data = []
    ) {
        $this->_coreRegistry = $registry;
        $this->_categoryNameFactory = $categoryNameFactory;
        $this->_categoryFactory = $collecionFactory;
        $this->_storeManager = $storeManager;
        $this->categoryImage = $categoryImage;
        $this->fileInfo = $fileInfo;
        $this->imageHelperFactory = $imageHelperFactory;
        $this->assetRepository = $assetRepository;
        parent::__construct($context, $data);
    }

    public function getEnableCategory()
    {

        $category = $this->_categoryFactory->create()->setStore($this->_storeManager->getStore());
        return $category;
    }

    public function getCategoryData($categoryId)
    {
        $category = $this->_categoryNameFactory->create()->load($categoryId)->setStore($this->_storeManager->getStore());
        return $category;
    }

    public function getCategoryImage($categoryData, $attributeCode)
    {
        $imageLink = null;
        $fileName = $categoryData->getData($attributeCode);
        if (($fileName) && ($this->fileInfo->isExist($fileName))) {
            $imageLink = $this->categoryImage->getUrl($categoryData, $attributeCode); //get category image
        } else {
            $imageHelper = $this->imageHelperFactory->create();
            $imageLink = $this->assetRepository->getUrl($imageHelper->getPlaceholder('image')); // get default placeholder image
        }

        return $imageLink;
    }

}

app\code\VendoreName\ModuleName\view\frontend\templates

cmscategory.phtml

<?php
$category = $block->getEnableCategory();
?>
<h1 align="center">Category  list</h1>
<?php foreach($category as $categorydata) : ?>
<?php
  $categoryid = $categorydata->getEntityId();
  $categoryFactory = $block->getCategoryData($categoryid);
  $categoryNameshow = $categoryFactory->getName();
  $categoryUrlShow = $categoryFactory->getUrl();
  $categoryImageurl = $block->getCategoryImage($categoryFactory, 'image');
  if ($categoryImageurl) {
?>
<div class="swiper-slide main-category-show-slide">
  <a class="category-link-show" href="<?php echo $categoryUrlShow; ?>" alt="<?php echo $categoryNameshow; ?>" title="<?php echo $categoryNameshow; ?>" >
        <div class="category-show-cmspage">
            <div class="category-slide">
                    <div class="category-img-show">
            <img src="<?php echo $categoryImageurl; ?> ">
          </div>
                    <div class="category-name-show"><?php echo $categoryNameshow; ?></div>
            </div>
        </div>
  </a>
</div>
<?php } ?>
<?php endforeach;?>

app\code\VendoreName\ModuleName\view\frontend\layout

cms_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">>
    <body>
        <referenceContainer name="content">
            <block class="VendoreName\ModuleName\Block\Categoryimage" template="VendoreName_ModuleName::cmscategory.phtml"/>
        </referenceContainer>
    </body>
</page>

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.