0

I have loaded the category and sub-category, now I need to load image as well but I'm not sure how to do that.

Below is the code I have used:

In block file:

public function getStoreCategories($sorted = false, $asCollection = false, $toLoad = true)
    {
        return $this->_categoryHelper->getStoreCategories($sorted , $asCollection, $toLoad);
    }

    public function getCategoryById($categoryId) 
    {
        return $this->_categoryRepository->get($categoryId);

    }

In phtm file:

<header class="headers">
  <div class="header-inrs">
    <div class="menu-container">
      <div class="menus contenedor-menu">
        <ul class="clearfix backcolor designmenu">
          <div class="totalmobilelog">
            <div class="lo">
              <a href="" ><b class="clsml"><i class="fa fa-user-plus" aria-hidden="true"></i>Login</b></a>
            </div>
            <div class="si">
              <a href="" ><b class="clsml"><i class="fa fa-registered" aria-hidden="true"></i>Sign Up</b></a>
            </div>
          </div>
          <?php
          $categories = $block->getStoreCategories();
          foreach ($categories as $category):

          ?>
          <li><a href="<?php echo $category->getRequestPath();?>"><?php echo $category->getName(); ?></a>
          <ul class="subc">
            <?php
            $subCategories = $block->getCategoryById($category->getId());
            foreach ($subCategories->getChildrenCategories() as $subCategory):
            ?>

            <li><a href="<?php echo $subCategory->getRequestPath();?>"><?php echo $subCategory->getName(); ?></a>
          </li>

          <?php endforeach; ?>
        </ul>
        <?php echo $block->getSubcategories($subCategories); ?>
        <?php endforeach; ?>
      </li>
  </ul>
</div>
</div>
</div>
</header>

Can you please help me with this?

0

To get Category Image url:

<?php echo $category->getImageUrl(); ?>

And use it in <img> tag like

<img src="<?php  echo $category->getImageUrl(); ?>" />

And get Alternate Image url:

echo $category->getImageUrl('category_alternate_image'); 
<img src="<?php  echo $category->getImageUrl('category_alternate_image'); ?>" />

I hope this will help

  • Thanks. Where do I need to put this code? Is it in phtml, right? – Siva Sep 30 at 5:36
  • @Siva Yes in phtml – Muhammad Hasham Sep 30 at 5:41
  • I have tried the code <?php echo $category->getImageUrl(); ?> in my phtml file but nothing happens. Where am I wrong? – Siva Sep 30 at 5:59
  • Have you put this <?php echo $category->getImageUrl(); ?> in <img> tag like I mentioned in my answer? if you did then either you are in wrong phtml or you category object is not properly loaded. – Muhammad Hasham Sep 30 at 6:48
  • I have put the code as you said and uploaded a category image via admin panel but still the image is not loading in the desired place. Can you please help? – Siva Oct 1 at 5:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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