How to Get Product current category Name in Product View Page above the Product Name. And give me file path Location to get Current Product Category Name in Magento 2.


Create a custom module like below code.

create a module block file to get current category name.

namespace Namespace\Modulename\Block;
class Blockname extends \Magento\Framework\View\Element\Template
    protected $_registry;

    public function __construct(
        \Magento\Backend\Block\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');


create a phtml file with below code

<?php if ($currentCategory = $block->getCurrentCategory()): ?>
    <div class="detail-category-name">
        <?php echo $currentCategory->getName(); ?>
<?php endif; ?>

call above phtml file via xml file.

<referenceContainer name="product.info.main">
            <block class="Namespace\Modulename\Block\Blockname" name="product.category.name" template="Magento_Catalog::product/view/yourfilename.phtml" >

And finally you can see category name just above product name on product detail page.

Let me know if you have any query.

  • i have Put xml code in catalog_product_view.xml in Product.info.main refrence but i cant see any div or any category name. – Nikul Jun 15 '16 at 12:41
  • You have to put it in your theme default.xml. – Suresh Chikani Jun 15 '16 at 13:21
  • Yes i have tried to Put in default.xml but its not working.. – Nikul Jun 17 '16 at 9:13
  • thanks. works like a charm. be careful at the path you're calling (mine was "Vendor_Categoryonproduct::category.phtml) and don't forget the registration and module file. – Claudiu Creanga Jul 18 '16 at 18:35
  • i am not getting any result – vijay b Feb 17 '17 at 11:56

You should be able to get the category Id's by using the getCategoryIds() method on a product. For example, reference:


This means that you should be able to essentially call $product->getCategoryIds() and filter through that in your block.

There is also a method called getCategoryCollection() in the same model of which would allow you to cycle through the categories as full objects. It should cut out the middle man of loading them with the Id's.

**Add XML Code Theme/namespace/Magento_Catalog/templates/product/view**

<block class="Magento\Catalog\Block\Product\View" name="product.info.category" after="product.price.final" template="product/view/current_category.phtml" />

**Create New File    Theme/namespace/Magento_Catalog/templates/product/view**  

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
        $categories = $product->getCategoryIds(); /*will return category ids array*/
        foreach($categories as $category){
            $cat = $objectManager->create('Magento\Catalog\Model\Category')->load($category);
            echo $cat->getName();


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.