cancel
Showing results for 
Search instead for 
Did you mean: 

Display Category Name on Product Page

SOLVED

Display Category Name on Product Page

Does anyone now how to display the category name on the individual product page. I would like it to be displayed after the product title. Each time I try I get a 500 server error.

 

I have tried placing code in: app/design/frontend/Theme/My_click_theme/Magento_catalog/templates/product/view/form.phtml.

 

I have also tried everything on the page including creating custom modules.

https://magento.stackexchange.com/questions/202423/how-to-display-category-name-in-product-page?rq=1

 

Any suggestions would be helpful.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Display Category Name on Product Page

Hello @mraccine ,

 

If you are putting code direct in phtml that might need to work as per reference link given in question.

By putting this code in app/design/frontend/Theme/My_click_theme/Magento_catalog/templates/product/view/form.phtml

 

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

 

 

If you still getting issue, then I suggest to call separate phtml file using app/design/frontend/Theme/My_click_theme/Magento_Catalog/layout/catalog_product_view.xml

 

You can put following code in xml file.

 

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
	<body>
		...
		...
		<referenceBlock name="page.main.title">
			<block class="Magento\Catalog\Block\Product\View" name="product.category.block" template="Magento_Catalog::product/view/customcategory.phtml" after="-" />
		</referenceBlock>
		
		...
		...
	</body>
</page>

 

 

and in create new phtml file with name customcategory.phtml  and add following code in app/design/frontend/Theme/My_click_theme/Magento_Catalog/templates/product/view/customcategory.phtml

 

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

If this helps then Click on Kudos and Accept as Solution.

 

Thank you

Hiren Patel

 

View solution in original post

2 REPLIES 2

Re: Display Category Name on Product Page

Hello @mraccine ,

 

If you are putting code direct in phtml that might need to work as per reference link given in question.

By putting this code in app/design/frontend/Theme/My_click_theme/Magento_catalog/templates/product/view/form.phtml

 

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

 

 

If you still getting issue, then I suggest to call separate phtml file using app/design/frontend/Theme/My_click_theme/Magento_Catalog/layout/catalog_product_view.xml

 

You can put following code in xml file.

 

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
	<body>
		...
		...
		<referenceBlock name="page.main.title">
			<block class="Magento\Catalog\Block\Product\View" name="product.category.block" template="Magento_Catalog::product/view/customcategory.phtml" after="-" />
		</referenceBlock>
		
		...
		...
	</body>
</page>

 

 

and in create new phtml file with name customcategory.phtml  and add following code in app/design/frontend/Theme/My_click_theme/Magento_Catalog/templates/product/view/customcategory.phtml

 

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

If this helps then Click on Kudos and Accept as Solution.

 

Thank you

Hiren Patel

 

Re: Display Category Name on Product Page

Thanks, I did need to create a separate phtml file to get it to work.