For this first let's create new tab for category display.
- Create file catalog_product_view.xml in the
app/design/frontend/{vender name}/{theme name}/Magento_Catalog/layout
In the file write the below code:
<?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="product.info.details">
<block class="Magento\Catalog\Block\Product\View" name="categoryinfo.tab" as="categoryinfo" template="product/view/category_info.phtml" group="detailed_info" >
<arguments>
<argument translate="true" name="title" xsi:type="string">Category</argument>
</arguments>
</block>
</referenceBlock>
</body>
</page>
- create the file category_info.phtml in the path
app/design/frontend/{vender name}/{theme name}/Magento_Catalog/templates/product/view.
In the file you can write any content like i have call all categories of this product.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categoryCollection = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');
$appState = $objectManager->get('\Magento\Framework\App\State');
$appState->setAreaCode('frontend');
$productId = $block->getProduct()->getId(); // YOUR PRODUCT ID
$product = $productRepository->getById($productId);
$categoryIds = $product->getCategoryIds();
$categories = $categoryCollection->create()
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', $categoryIds);
foreach ($categories as $category) {
$name = $category->getName();
$parentCategories = $category->getParentCategories();
foreach($parentCategories as $parent){
$parentname = $parent->getName();
echo '<a href="'.$parent->getUrl().'" title="'.$parentname.'" alt="'.$parentname.'">';
echo '<div>'.$parentname.'</div>';
echo '</a>'; echo ' > ';
}
echo '<a href="'.$category->getUrl().'" title="'.$name.'" alt="'.$name.'">';
echo '<div>'.$name.'</div>';
echo '</a>';
if($category->hasChildren()) {
$subcategories = $category->getChildrenCategories();
foreach($subcategories as $subcat){
$subname = $subcat->getName();
echo ' > ';
echo '<a href="'.$subcat->getUrl().'" title="'.$subname.'" alt="'.$subname.'">';
echo '<div>'.$subname.'</div>';
echo '</a>';
}
}
}