Skip to content

How to display children of subcategories

I wrote a function that outputs the current category and its children in a certain order, like so:

function Orderlist($param2) {
    $objectManager = MagentoFrameworkAppObjectManager::getInstance();
    $category = $objectManager->get('MagentoFrameworkRegistry')->registry('current_category');//get current category    
    $catId =$category->getId();        
    $subcategory = $objectManager->create('MagentoCatalogModelCategory')->load($catId);           
    $subcats = $subcategory->getChildrenCategories();

foreach ($subcats as $subcat) {
            if ($subcat->getIsActive() and str_starts_with($subcat->getName(), $param2)) {
                $subcat_url = $subcat->getUrl(); ?>
                <a href="<?php echo $subcat_url; ?>"> <?php echo $subcat->getName(); ?> </a>
<?php } } } ?> 

But now I’m trying to display the subcategories their children.

Current frontend:

enter image description here

For example: “Alpha” in the image above has 2 subcategories which I would like to display.
enter image description here

What would be the best way to achieve this?

Thanks in advance!!