How to get Sub Category details by parent id Magento 2?

Get Sub Category (Child category) Collection details by the Parent Category id in Magento 2.

Using Parent Category Id, You can explore all the children category with deep level Or You can retrieve id by Current Category

When you check the Magento Open source sample data, You can see Men Category has direct two-child categories, Tops, and Bottoms at level 3 and both have other subcategories (Jacket, Tees, Pants, Shorts, etc) at level 4.

<?php
namespace Jesadiya\SubCategory\Block;

use Magento\Catalog\Api\Data\CategoryTreeInterface;
use Magento\Catalog\Api\CategoryManagementInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Psr\Log\LoggerInterface;

class SubCategory extends Template
{
    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @var CategoryManagementInterface
     */
    private $categoryManagement;

    public function __construct(
        Context $context,
        CategoryManagementInterface $categoryManagement,
        LoggerInterface $logger,
        array $data = []
    ) {
        $this->categoryManagement = $categoryManagement;
        $this->logger = $logger;
        parent::__construct($context, $data);
    }

    /**
     * @param int $categoryId
     * @return array
     */
    public function getSubCategoryByParentID(int $categoryId): array
    {
        $categoryData = [];

        $getSubCategory = $this->getCategoryData($categoryId);
        foreach ($getSubCategory->getChildrenData() as $category) {
            $categoryData[$category->getId()] = [
                'name'=> $category->getName(),
                'url'=> $category->getUrl()
            ];
            if (count($category->getChildrenData())) {
                $getSubCategoryLevelDown = $this->getCategoryData($category->getId());
                foreach ($getSubCategoryLevelDown->getChildrenData() as $subcategory) {
                        $categoryData[$subcategory->getId()]  = [
                            'name'=> $subcategory->getName(),
                            'url'=> $subcategory->getUrl()
                        ];
                }
            }
        }

        return $categoryData;
    }

    /**
     * @param int $categoryId
     * @return CategoryTreeInterface|null
     */
    public function getCategoryData(int $categoryId): ?CategoryTreeInterface
    {
        try {
            $getSubCategory = $this->categoryManagement->getTree($categoryId);
        } catch (NoSuchEntityException $e) {
            $this->logger->error("Category not found", [$e]);
            $getSubCategory = null;
        }

        return $getSubCategory;
    }
}

Using a given code snippet, you can retrieve all the subcategories of the parent without depends on the level type.

Call Block Class methods in the template,

$parentID = 11; //MEN Category Id
$getCategoryList = $this->getSubCategoryByParentID($parentID);

<?php if (count($getCategoryList)) { ?>
    <?php foreach ($getCategoryList as $id => $category) : ?>
            <a href="<?= $category['url'] ?>">
                <?= $category['name']; ?>
            </a>
    <?php endforeach;  ?>
<?php }  ?>

You can get all the subcategory details by the given approach.

The output will display all the Child Category of the Specific Parent Category with name, URL, status, and id.

You can also read Get Category Collection Per Store level in Magento 2.