Home  >  Q&A  >  body text

Can't display category images in my create module

I want to display caregory image in my own module, so I created a module and frontend/templates/category-section.phtml, here is the code, my problem is that the source image is empty, it is in my Returns null in the module to create the page, all other similar properties work fine

I uploaded the image in the admin panel and when I click on the a tag it correctly returns the category page with the image but only in my display category and image the image is not showing

Image not shown here

If I click on any of them, the URL and the image are there, but not in the new module

<?php echo $category->getName() ?>
<section class="section-holder">
    <div class="all-categories">
        <p class='category-text'>categories</p>
        <div class='categories-flex'>
            <?php
            $categoryHelper = $this->helper('Magento\Catalog\Helper\Category');
            $outputhelper = $this->helper('Magento\Catalog\Helper\Output');


            foreach ($categoryHelper->getStoreCategories() as $category) :

            ?>
                <a href="<?php echo $categoryHelper->getCategoryUrl($category) ?>">
                    <div class='child-category'>
                        <div>
                            <img style="background-image: url('<?= $category->getImageUrl(); ?>');" alt="">
                        </div>

                        <p> <?php echo $category->getName() ?></p>
                    </div>
                </a>
            <?php
            endforeach;
            ?>
        </div>
    </div>
</section>

P粉731977554P粉731977554259 days ago577

reply all(1)I'll reply

  • P粉458725040

    P粉4587250402024-02-27 13:13:04

    MethodgetStoreCategoriesDoes not load images for categories. It only loads attributes declared in the active module's catalog_attributes.xml file.
    You can try adding image attributes to this list by creating this file in your own module

    etc/catalog_attributes.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd">
        <group name="catalog_category">
            <attribute name="image"/>
        </group>
    </config>

    Clear cache when finished.

    reply
    0
  • Cancelreply