I am fairly new to Magento, and am having trouble figuring out what I need to add to do this. I have looked all over for this, but can not find a solution that shows how to do this for magento 2. I am looking for how to create a module that displays all of the subcategories of the category clicked. If there are no more subcategories, then I want to display the products for that category.

I have tried making blocks in the admin panel, and attaching these blocks to the product category. I would really like to just create a module that does this for me though.

2 Answers 2


You need to customized the category in the theme folder use the following file structure and update the following code



// call current category and sub category

$categoryHelper_sub = $this->helper('Magento\Catalog\Helper\Category');
$_category  = $block->getCurrentCategory();

$objectManager_sub = $objectManager =
$imagehelper = $objectManager->create('Magento\Catalog\Helper\Image');
$category = $objectManager_sub->get('Magento\Catalog\Model\Category');
$categories_sub =
1)->addIdFilter($_category->getChildren())->setOrder('position', 'ASC');?>

<?php if(!empty($categories_sub->getData())){ ?>
<div class="page-products">
        <div class="products wrapper grid products-grid">
                <div class="col-xs-12 abt_head caps">
                        <h2><?php echo $_category->getName(); ?></h2>
                        <p><?php echo $_category->getDescription(); ?></p>
                        <div class="current-image">
                                        <img src="<?php echo $_category->getImageUrl()?>" >
                        <?php foreach($categories_sub as $category_sub): ?>
                                <?php $category_sub->getParentId();
                                $categoryobj =
                                $category =
current category
                                <div class='submenu'>
                                        <a href="<?php echo
                                        <div class='submenu-img'> <img src="<?php echo
$category_sub->getImageUrl()?>" alt="<?php echo
                                        <?php //echo $category_sub->getName()?></a>
                        <?php endforeach; ?>
<?php } ?>

I have create small module for display sub category please check this link.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.