I am trying to fetch current subcategory name of specific parent category which id is 5 for example my category is Brand and subcategory is Brand1/Brand2/brand3 so i want fetch current subcategory category on product page .

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

enter image description here


To get sub-category colletion for specific category, you need to used following code.

$categoryId = 5;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$current_category = $objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);

// Get Sub-Category For Current Categories

$subcategories = $parent_category->getChildrenCategories();
foreach($subcategories as $subcategorie) 
   echo $subcategorie->getName();
  • Dhiren Vasoya@ I am trying to print category name on product details page,I have brand and brand have many subcategory, when user click on brand subcategory then need to show only current subcategory.? – sanjay Jul 4 '17 at 11:05
  • @sanjay you need product category? Check your question code, you are trying to get category from Registry, and it return when you are on category page. – Dhiren Vasoya Jul 4 '17 at 11:07
  • Above code need on;y perform of specific category which ID is 5? can you help me – sanjay Jul 4 '17 at 11:08
  • Can you clerify your requirement in detail. What you actualy want? – Dhiren Vasoya Jul 4 '17 at 11:09
  • @sanjay are you looking to get sub-categories of main category where main category Id is 5? something like this? – Dhiren Vasoya Jul 4 '17 at 11:11

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.