Actually, I want to get a current category, subcategory name, and URL.

Below code is not useful...

 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.


I found this code and it is working fine for some products but for some products it's not working...


    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');
    $catName = $category->getName(); // to get the category name
    $catUrl = $category->getUrl(); // to get the category Url
  • Can you update your full code? Sep 14 '18 at 11:59

Following way you get Subcategories by category Id.

public function __construct(
    \Magento\Catalog\Model\CategoryFactory $categoryFactory
) {
    $this->categoryFactory = $categoryFactory;

$categoryId = 4;
$category = $this->categoryFactory->create()->load($categoryId);

$subcategories = $category->getChildrenCategories();
foreach ($subcategories as $subcategorie) {
    echo $subcategorie->getId();
    echo $subcategorie->getName();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
      $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');
  • why registry is working for some products but for some products its not working Sep 14 '18 at 14:49

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.