I want to show all the category on the left sidebar on home page. Please let me know how to get all the categories by collection, or there is another way to call all the category. please let me know


Here is open source extension for do that - you can use code and modify for your needs Magento 2 Frontend : How to call category collection on home page Explained in blog post

| improve this answer | |
  • 1
    Awesome FireBear. Its work for me. thanks for giving such attention – sandeep sharma Oct 12 '15 at 5:25
  • How to show category images I am using $category->getImageUrl(); but its not working – Pradeep Singh Feb 15 '16 at 10:43
  $categoryHelper = $this->helper('Magento\Catalog\Helper\Category');
  foreach($categoryHelper->getStoreCategories() as $category): 
  <li><a href="<?php echo $categoryHelper->getCategoryUrl($category) ?>"><?php echo $category->getName() ?></a></li>
<?php endforeach; ?>
| improve this answer | |

Check below code:

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

                    $categories = $categoryFactory->create()                              

foreach ($categories as $category):     
echo $category->getName();
| improve this answer | |
  • 1
    Btw, it's not recommended to use object manager directly for creating objects. Use DI instead. – MagePsycho May 25 '16 at 6:43
  • Is there any way to get the products of current category like above? If any than please share.. – Yudi Oct 27 '16 at 13:06

You need to use DI in your class - 1. add property of AdminSession class to class 2. add including of the variable using dependensy injection:

 * @var \Magento\Store\Model\StoreManagerInterface

protected $_storeManager;

public function __construct(
    \Magento\Store\Model\StoreManagerInterface  $_storeManager
    $this->_storeManager = $_storeManager;
$store = 1;//store id
public function getCategories($store){
    $rootCategoryId = $this->_storeManager->getStore($store)
    $collection = $this->_categoryCollectionFactory->create();
        ->addAttributeToFilter('path', array('like' => "1/{$rootCategoryId}/%"))

    foreach ($collection as $category) {
        $categories[] = array(
    return $categories;
| improve this answer | |

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.