I have a product which have assigned into more than 5 categories. I want to show the category detail of that product on product detail page. But which category detail I need to show?

I want to show the category name of that category from where the user coming.

Can anyone suggest the solution?


2 Answers 2


You have to have created a module to show a current category on the product page. Check below files




<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.0">


namespace Vendor\Module\Block;

class Module extends \Magento\Framework\View\Element\Template

     * Constructor
     * @param \Magento\Framework\View\Element\Template\Context  $context
     * @param array $data
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Registry $registry,
        array $data = []
    ) {
        $this->_registry = $registry;
        parent::__construct($context, $data);

    public function getCurrentCategory()
        return $this->_registry->registry('current_category');


 * @var $block \Vendor\Module\Block\Module
<div class="category-name">
    <?php if($block->getCurrentCategory()): ?>
        <h3><?= __('Category') ?>: <?= $block->getCurrentCategory()->getName();; ?></h3>
    <?php endif; ?>


<?xml version="1.0" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceContainer name="product.info.main">
            <block class="Vendor\Module\Block\Module" name="current.category.on.product" template="Vendor_Module::currentCategory.phtml" before="-"/>

Check the below screenshot: enter image description here

Hope this will work for you.

  • Thanks @kishor. I think this is working. Commented Jan 23, 2020 at 5:23
  • Please upvote the answer if its helpfull to you. Commented Jan 23, 2020 at 5:26

If you want to get the current product category id then use below code

NOTE: Please do not use the Object manager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Block\Product\View')->getProduct();
$categoryId = $product->getCategoryId();
$category = $objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);
echo $cat->getName();
/*echo "<pre>";

I hope this is helpful to you!!

  • Why you added the note to not use the Object manager while your solution is using it? Commented Mar 16, 2021 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.