magento2magento-2.0

How to get current category in magento2?


How can i get current category in magento2 ?

I want to get category name and category id in custom phtml file.


Solution

  • Try this code. this will definitely help you.

    <?php 
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
        echo $category->getId();
        echo $category->getName();
    ?>