- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 }
}
}
?>
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Display subcategories in parent category
Hello,
Please can anyone tel us where to put all this code?
Desperate here
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Display subcategories in parent category
<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category ?> <?php $requestInterface = $objectManager->get('Magento\Framework\App\RequestInterface'); //echo $routeName = $requestInterface->getRouteName(); //echo "<br/>"; //echo $moduleName = $requestInterface->getModuleName(); //echo "<br/>"; $controllerName = $requestInterface->getControllerName(); //echo "<br/>"; //echo $actionName = $requestInterface->getActionName(); ?> <?php $parcatId = $category->getId(); // current Category ID $parcategory = $objectManager->create('Magento\Catalog\Model\Category')->load($parcatId); $parent_category = $parcategory->getParentCategory(); $subcatId = $parent_category->getId(); $subcategory = $objectManager->create('Magento\Catalog\Model\Category')->load($subcatId); $subcats = $subcategory->getChildrenCategories(); $_helper = $this->helper('Magento\Catalog\Helper\Output'); ?> <?php if($parent_category->getLevel() == 2 || $controllerName == 'product'): ?> <ul class="sub_cat_bg navigation"> <?php foreach ($subcats as $subcat) { //$_category = $objectManager->create('Magento\Catalog\Model\Category')->load($subcat->getId()); //$_outputhelper = $this->helper('Magento\Catalog\Helper\Output'); $subcaturl = $subcat->getUrl(); ?> <li class="cat_image_bg level0"> <a class="level-top" href="<?php echo $subcaturl ?>"> <span><?php echo $subcat->getName(); ?></span> </a> </li> <?php } ?> </ul> <?php endif; ?>
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Display subcategories in parent category
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category <?php $catId = $category->getId(); // Parent Category ID $subcategory = $objectManager->create('Magento\Catalog\Model\Category')->load($catId); $subcats = $subcategory->getChildrenCategories(); $categoryHelper = $this->getCategoryHelper(); //$this->getChildCategories($subcategory) //$_helper = $this->helper('Magento\Catalog\Helper\Output'); //echo "<pre>"; //print_r($subcats->count()); //echo "<pre>"; $count = 0; ?> <div class="category_bg mobile"> <ul id="main_cat_bg" class="main_cat_bg"> <?php $cat_togg = 0; foreach ($subcats as $subcat) { if (!$subcat->getIsActive()) { continue; } $_category = $objectManager->create('Magento\Catalog\Model\Category')->load($subcat->getId()); //$_outputhelper = $this->helper('Magento\Catalog\Helper\Output'); $helper = $this->helper('SR\CategoryImage\Helper\Category'); $subcaturl = $subcat->getUrl(); $imageUrlthum = $helper->getImageUrl($_category->getData('thumbnail')); //$imageUrlthum = resize($_category->getData('thumbnail'),153,153); //$cat_desc = $_category->getCatDescription(); $_imgHtml = ''; if ($imageUrlthum) { $_imgHtml = '<img src="' . $imageUrlthum. '" />'; //$_imgHtml = $_outputhelper->categoryAttribute($_category, $_imgHtml, 'image'); /* @escapeNotVerified */ } ?> <li> <div class="sub_cat_content_main"> <div class="cat_image_text"> <a href="<?php echo $subcaturl ?>"> <?php echo $_imgHtml;?> <!--<div class="desicription_part">--> <?php //echo $cat_desc; ?> <!--</div>--> </a> <div class="sub_name_bg"> <a href="<?php echo $subcaturl ?>"> <?php echo $subcat->getName(); ?> </a> </div> <!-- Start 3rd Level Chiled Category--> <?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $object_managertwo = $objectManager->create('Magento\Catalog\Model\Category')->load($subcat->getId()); $subcatslevelthird = $object_managertwo->getChildrenCategories(); ?> <?php if ($subcatslevelthird->count() > 0) { ?> <ul class="sub_cat_bg"> <?php foreach ($subcatslevelthird as $subcatthird) { $_outputhelper = $this->helper('Magento\Catalog\Helper\Output'); $subcaturl = $subcatthird->getUrl(); ?> <li class="cat_image_bg"> <a class="level-top" href="<?php //echo $subcaturl ?>"> <span><?php //echo $subcatthird->getName(); ?></span> </a> <div class="child_name_bg"> <span><?php echo $subcatthird->getName(); ?></span> </div> <!-- Start 4th Level Chiled Category--> <?php $_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $object_managerthree = $_objectManager->create('Magento\Catalog\Model\Category')->load($subcatthird->getId()); $subcatslevel = $object_managerthree->getChildrenCategories(); ?> <?php if ($subcatslevel->count() > 0){?> <ul class="chiled_cat_bg"> <?php foreach ($subcatslevel as $subcatlevel) { $_outputhelper = $this->helper('Magento\Catalog\Helper\Output'); $subcaturl = $subcatlevel->getUrl(); ?> <li class="cat_image_bg"> <a class="level-top" href="<?php echo $subcaturl ?>"> <span><?php echo $subcatlevel->getName(); ?></span> </a> </li> <?php } ?> </ul> <?php } ?> <!-- End 4th level Chiled Category--> </li> <?php } ?> </ul> <?php } ?> <!-- End 3rd level Chiled Category--> </div> </div> </li> <?php } ?> </ul> </div>
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Display subcategories in parent category
If you are looking for a much better solution to show subcategories on category view pages on Magento2 frontend, Have a look at Advanced Subcategory Grid module on Marketplace.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Display subcategories in parent category
Try this blog for your requirements
http://magecomp.com/blog/display-subcategories-information-category-page-magento-2/ |
200+ Premium Magento 2 Extensions Need help? Hire Magento Developer