3

Want to display the category name for each product in new products widget and best sellers widget

I have tried this but not working

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');
echo $category->getname();
PЯINCƎ
  • 11,669
  • 3
  • 25
  • 80
Rathna
  • 303
  • 1
  • 6
  • 18

2 Answers2

3
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product       = $objectManager->create('Magento\Catalog\Model\Product')->load($_item->getId());
$categories    = $product->getCategoryIds();
$cat_name      = array();
foreach ($categories as $category) {
    $cat        = $objectManager->create('Magento\Catalog\Model\Category')->load($category);
    $cat_name[] = $cat->getName();
}
echo "<pre/>";
print_r($cat_name);
Kushal Dani
  • 2,114
  • 2
  • 17
  • 49
2

Try This

  <?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();
?>
Birjitsinh Zala
  • 1,167
  • 6
  • 19