I need to show 6 specific categories on my Magento home page. I have the category ids with me. I want to display both the category image and name of that specific category. I have created a category.phtml and have the following code in this file.

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

And called this phtml in cms home page as:

{{block class="Magento\Framework\View\Element\Template" template="Vendor_Theme::category.phtml"}}

But it is showing only the root categories. Please help.

  • have you all six category id? Mar 28 '17 at 4:53
  • yes.I have all category ids
    – Vindhuja
    Mar 28 '17 at 5:34

Better idea create a custom module for this.

This module must have below files:

  • app\code\[VendorName]\[ModuleName]\composer.json
  • app\code\[VendorName]\[ModuleName]\registration.php
  • app\code\[VendorName]\[ModuleName]\etc\module.xml
  • app\code\[VendorName]\[ModuleName]\Block\Categories.php

On block class, you should create category collection filter and filter that collection by your 6 category ids

namespace [VendorName]\[ModuleName]\Block;
class Categories extends \Magento\Framework\View\Element\Template

  protected $_categoryCollectionFactory;
  protected $_categoryHelper;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
        \Magento\Catalog\Helper\Category $categoryHelper,
        array $data = []
        $this->_categoryCollectionFactory = $categoryCollectionFactory;
        $this->_categoryHelper = $categoryHelper;
        parent::__construct($context, $data);

    public function getCategories()
        $collection = $this->_categoryCollectionFactory->create();
        $categoryIds = array(CatId1,CatId2,...CatId6);
        return $collection

Then use this new block instead of Magento\Framework\View\Element\Template.

See idea how use block class at http://blog.chapagain.com.np/magento-2-get-list-of-all-categories-store-categories/


You can used following code to get category collections.

$objectManager= \Magento\Framework\App\ObjectManager::getInstance();
$categoryFactory = $objectManager->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
    $categories = $categoryFactory->create()                              

    foreach ($categories as $category)
        $_category = $objectManager->create('Magento\Catalog\Model\Category')->load($category->getId());
        $_imgUrl = $_category->getImageUrl();
  • Please help me to get category image.
    – Vindhuja
    Mar 28 '17 at 7:25
  • I update my answer with logic for category update. And if it help you, please accept, so it will help other in community as well. Mar 28 '17 at 9:07

If you have category ids, then you can directly call that category using below code.


$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->create('Magento\Catalog\Model\Category')->load($cat_id);

<img src="<?php echo $category->getImageUrl(); ?>" />
  • On printing $category, I got a huge array.My system got hang.Is it possible to get category name and image only from that array.
    – Vindhuja
    Mar 28 '17 at 6:13
  • Please help me to get category image
    – Vindhuja
    Mar 28 '17 at 7:25
  • Check my updated answer. It may help you. Mar 28 '17 at 11:20

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.