cancel
Showing results for 
Search instead for 
Did you mean: 

Display subcategories in parent category

Display subcategories in parent category

I would like to display subcategories in parent category. I use Magento 2.0.4.

How can I get the title and the image of the subcategories on a categorie page?

5 REPLIES

Re: Display subcategories in parent category

<?php   /*call you block file to get category collection */
$yourBlock = \Magento\Framework\App\ObjectManager::getInstance()->get('Namespace\Modulename\Block\CategorisCollection');

 

$categories = $yourBlock ->getStoreCategories(true,false,true);
$categoryHelper = $yourBlock->getCategoryHelper();

foreach($categories as $category) {

     if (!$category->getIsActive()) {
        continue;
     }

?>
    <li > <a href="<?php echo $categoryHelper->getCategoryUrl($category) ?>" ><?php echo $category->getName() ?></a>

 <?php  

if($childrenCategories = $yourBlock ->getChildCategories($category)){
    ?>
        <ul >
        <?php
        foreach($childrenCategories as $childrenCategory){
             if (!$childrenCategory->getIsActive()) {
                continue;
             }
        ?>
            <li><a href="<?php echo $categoryHelper->getCategoryUrl($childrenCategory) ?>"><?php echo $childrenCategory->getName() ?></a></li>

<?php }
 }
 }
 ?>

 

 

Re: Display subcategories in parent category

I'd like to know also.

 

I currently can get subcategories, titles and urls but cannot get the image!

 

Does anyone know how to get the image url of the category?

 

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');
$subcats = $category->getChildrenCategories();
?>
<div class="row">
    <?php
    foreach ($subcats as $subcat) {
        if ($subcat->getIsActive()) {
            
            $subcat_url = $subcat->getUrl();
            $subcat_img = "";
            $placeholder_img = "/media/placeholder.png";

            ?>
            <div class="col-sm-6 sub-cat-tile">
            	<a href="<?php echo $subcat_url; ?>">
            		<img src="<?php echo $subcat->getImageUrl() ? $subcat_img : $placeholder_img; ?>">
            		<p><?php echo $subcat->getName(); ?></p>
            	</a>

            </div>
            <?php
        }
    } ?>
</div>

getImageUrl() only returns a boolean, and doesnt even return true if a category has an image!

Re: Display subcategories in parent category

Ah, this was solved easily by adding this into the loop.

 

$subcategory = $objectManager->create('Magento\Catalog\Model\Category')->load($subcat->getId()); 
            

$subcat_img = $subcategory->getImageUrl();

Hope this helps.

Re: Display subcategories in parent category

Hello,

Please can anyone tel us where to put all this code?

Desperate hereSmiley Happy

Re: Display subcategories in parent category


Amazing post !! that is really helpful content.

For more helpful content see more helpful conent regarding Magento 2 Get Sub-categories.