7

I have found several methods of obtaining the product id of a category while on a category list page, but none covering the best way to obtain the parent name of the category while on a category page. Any help is appreciated.

3 Answers 3

8

I found this to be the quicker solution to retrieving the parent of the category.

<?php 
   $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
   $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
   echo $category->getParentCategory()->getName();
?>
2
  • This also doesn't work as it should for me - it doesn't get $category if I am on some category page. You know why?
    – Anitr
    Sep 5, 2017 at 13:44
  • 1
    Magento now recommends we not use object manager directly. You should create a module using the solution provided by Amit Bera. Sep 6, 2017 at 19:29
4

In Magento2,if you have an instance of category object then you can get its parent category name by calling $categoryObject->getParentCategory->getName().

If you want to show the parent category name on the phtml template of the category page then, in the Block class assigned to the phtml template,you need to inject dependency to initialize magento registry class \Magento\Framework\Registry in the Block class' __construct() and from current_category get the current category object

    protected $_registry;

     /**
      * @param \Magento\Framework\Registry $registry,
     */

        public function __construct(
            ...,
            ...,
            \Magento\Framework\Registry $registry,
            ...
        ) {
            $this->_registry = $registry;
            ...
            ...
        }

/**
 * Return catalog current category object
 * @return \Magento\Catalog\Model\Category
 */

public function getCurrentCategoryOb()
{
    return $this->_registry->registry('current_category');
}

Now at phtml file call this code for getting parent category name:

if($this->getCurrentCategoryOb()):
if($this->getCurrentCategoryOb()->getParentCategory()):
echo  $this->getCurrentCategoryOb()->getParentCategory())->getName();
endif
endif;
10
  • Where does the class code go? Feb 14, 2016 at 9:01
  • Can u please tell where are u want to show the code, exact section of category
    – Amit Bera
    Feb 15, 2016 at 8:01
  • Above the page-title block. Feb 15, 2016 at 22:40
  • Can u please give me screen shot of webpage where you want to add this
    – Amit Bera
    Feb 16, 2016 at 17:48
  • if I understand your question right, you want to get the product name on a category page? You can get the product name inside the product_list for each product like this: github.com/magento/magento2/blob/… But, if you want to display the name in the page-title, how should magento know, which product of the category should go in the page title? Feb 16, 2016 at 17:50
1

Use the following code to resolve your query:

<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
      $current_category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');
      $parent_category = $current_category->getParentCategory();
      echo $current_category->getName();
      echo $parent_category->getName(); ?>
2
  • This doesn't work as it should for me - it doesn't echo current category if I am on some category. Instead, it displays as current category is some of parent categories.. You know why?
    – Anitr
    Sep 5, 2017 at 13:43
  • When category is selected by clicking on filters on layered navigation, it doesn't work.
    – Anitr
    Sep 6, 2017 at 12:45

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.