Monday, August 22, 2022

[FIXED] How to get current category in magento2?

Issue

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();
?>


Answered By - Makwana Ketan
Answer Checked By - David Marino (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.