---
title: "How to get sub category details using main category URL key in Magento 2"
url: "https://magecomp.com/blog/get-sub-category-details-using-main-category-url-key-magento-2/"
date: "2018-06-04T17:52:26+00:00"
modified: "2025-03-11T11:42:27+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 404
reading_time: "3 min read"
summary: "In the early days of Ecommerce, products are listed directly on the homepage either in list view, and remaining options or filters displayed in a left-column. But over a time with growing use of th..."
description: "To make your Magento 2 store product easily available for your customers from available millions of options, store owners need to split products into differe..."
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Reset Customer Password using Console Command in Magento 2?"
    url: "https://magecomp.com/blog/reset-customer-password-using-console-command-magento-2/"
  - title: "How to Get Order Information Using SOAP API in Magento 2"
    url: "https://magecomp.com/blog/get-order-information-using-soap-api-magento-2/"
  - title: "How to Override core code using Custom Plugin in Magento 2"
    url: "https://magecomp.com/blog/override-core-code-using-custom-plugin-magento-2/"
---

# How to get sub category details using main category URL key in Magento 2

_Published: June 4, 2018_  
_Author: Dhiren Vasoya_  

![How to get subcategory details using main category urlkey](https://magecomp.com/blog/wp-content/uploads/2018/06/How-to-get-subcategory-details-using-main-category-urlkey-1024x332.webp)

In the early days of Ecommerce, products are listed directly on the homepage either in list view, and remaining options or filters displayed in a left-column. But over a time with growing use of the internet, people are more tending to shop online instead of walking to the nearest shop. To comply with the trend and increase online shopping sales by serving tons of options to customer needs, shop owners started dividing shop products into multiple categories and subcategories that make easy for the customer to find a product from millions of product options.

[![Hire Magento Programmer](https://magecomp.com/blog/wp-content/uploads/2025/02/Hire-Magento-Programmer-CTA-5.webp)](https://magecomp.com/services/hire-magento-programmer/)

If Shop owner doesn’t split products into proper categories, store shoppers need to suffer a lot to find a product from the list of options. Also, creating too many categories, subcategories might confuse your customers. To reduce the efforts of shoppers and to implement, easy navigations to throughout the whole store, you can use graphics tiles with suitable category image. Because these categories and subcategories pages are means a lot to gain better ranking over SERP pages we decided to write the blog to display subcategory details under the hood of main category URL key. You can even use this code to find subcategories of the main category.

[![Magento Malware Removal](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-Malware-Removal-CTA-6.webp)](https://magecomp.com/magento-malware-removal.html)

Firstly, we need to create custom extension and place given code to ‘index.php’ file at below location.
**app\code\Vendor\Extension\Block\Index\Index.php**

```
<?php
namespace VendorExtensionBlockIndex;

class Index extends MagentoFrameworkViewElementTemplate
{
   protected $_categoryFactory;

   public function __construct(
      MagentoCatalogModelCategoryFactory $categoryFactory,
      MagentoCatalogBlockProductContext $context,
      array $data = []
   ){
      $this->_categoryFactory = $categoryFactory;
      $this->_storeConfig = $scopeConfig;
      $this->_registry = $registry;
      parent::__construct($context, $data);
   }
   public function getSubcategoryByUrlkey($url_key)
   {
      $categorys = $this->_categoryFactory->create()
      ->getCollection()
      ->addAttributeToFilter('url_key',$url_key)
      ->addAttributeToSelect('*');
      $cat_id = $categorys->getFirstItem()->getEntityId();

      $category = $this->_categoryFactory->create()->load($cat_id);
      $childrenCategories = $category->getChildrenCategories();
      $allChildIds = $childrenCategories->getAllIds();

      $newCollection = $this->_categoryCollectionFactory->create()
      ->addFieldToFilter('entity_id',array('in' => $allChildIds))
      ->addAttributeToSelect('*');
      foreach($newCollection as $subcategorie) {
         echo '+'.$subcategorie->getName().'+'.$subcategorie->getDescription();
      }
   }
}
```

You can play & use this code according to your need of displaying subcategory in respective CMS pages of your Magento 2 store. Tell us in comment section, how you have used this code in your Magento 2 store.

[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2023/10/Magento-2-Speed-Performance-Optimization-Services-CTA-4.webp)](https://magecomp.com/magento-performance-optimization.html)

Happy Splitting!


---

_View the original post at: [https://magecomp.com/blog/get-sub-category-details-using-main-category-url-key-magento-2/](https://magecomp.com/blog/get-sub-category-details-using-main-category-url-key-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-09-07 11:17:23 UTC_  
