how to get category collection using product id in product details page in magento 2?

vijay picture vijay · May 13, 2016 · Viewed 7.3k times · Source

How to get category collection in product details page. I've tried using below code but it's not getting the collection. how to get it?

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $category = $objectManager->get('Magento\Framework\Registry')
        ->registry('current_category');
    $categoryProducts = $category->getCategoryProducts($categoryId);

Answer

Emizen Tech picture Emizen Tech · May 14, 2016

on product detail page you can just call getCategoryCollection function on product's Object to get Category Collection as following :

$_product = $block->getProduct();
$categoryCollection = $_product->getCategoryCollection();