cancel
Showing results for 
Search instead for 
Did you mean: 

How to get all First Level categories in magento2

How to get all First Level categories in magento2

i want to display all the first level categories only. How can i acheive it magento2?

 

8 REPLIES 8

Re: How to get all First Level categories in magento2

Hi @ayesha_khalid,

Do you want to show any specific page?

If yes, then you can get using collection with the parent id.
https://magento.stackexchange.com/a/196155

If you want to hide from top menu then you can manage admin category edit page as well.

I hope it will help you!

Re: How to get all First Level categories in magento2

Hello @ayesha_khalid 

 

There are two methods for the same:

 

Method 1:

The programmatic method at https://magento.stackexchange.com/questions/196145/magento-2-how-to-get-first-level-parent-category-...

 

Method 2:

If you want to display the first level categories in the menu, follow https://docs.magento.com/m2/ce/user_guide/catalog/navigation-top.html

 

Hope it helps.

---
If you've found my answer useful, please give"Kudos" and "Accept as Solution"

Re: How to get all First Level categories in magento2

@ayesha_khalid 

you can get using the below code in your phtml file

 

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$category_model = $objectManager->get('Magento\Catalog\Model\Category');

$category_id = 2//default category

$category = $category_model->load($category_id);
$subcategories = $category->getChildrenCategories();
$subcat_array = array();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
foreach($subcategories as $key => $subcategory) {
$subcat_array[$alphabet][$key]['name']= $subcategory->getName();
$subcat_array[$alphabet][$key]['url']= $subcategory->getUrl();
}
If it help you accept this as solution and give kudos.

Re: How to get all First Level categories in magento2

Hi Vimal

I want to display the first level categories only like HPE, CISCO, WD on another CMS PAGE.

unnamed.png

Re: How to get all First Level categories in magento2

Hi piyush i am using your code in my phtml file and i am getting this error

1.PNG

Re: How to get all First Level categories in magento2

@ayesha_khalid 
your image not  visible can you paste the error here?

Re: How to get all First Level categories in magento2

@ayesha_khalid 

just show what code you write as i am using the same code in my site it work fine 

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$category_model = $objectManager->get('Magento\Catalog\Model\Category');

$category_id = 2;//default category

$category = $category_model->load($category_id);
$subcategories = $category->getChildrenCategories();
$subcat_array = array();
foreach($subcategories as $key => $subcategory) {
$subcat_array[$alphabet][$key]['name']= $subcategory->getName();
$subcat_array[$alphabet][$key]['url']= $subcategory->getUrl();
}
If it help you accept this as solution and give kudos.

Re: How to get all First Level categories in magento2

Hello @ayesha_khalid,

If you want to use the first level categories in static cms pages, the best approach is to create a custom module and then use it's phtml in static pages. Firstly create module as:

Step 1. Create registration.php in app/code/Vendor_Name/TopLevelCategory/

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Name_TopLevelCategory',
    __DIR__
);

Step 2. Create module.xml in app/code/Vendor_Name/TopLevelCategory/etc/

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Name_TopLevelCategory" />
</config>

Step 3. Create categories.phtml in app/code/Vendor_Name/TopLevelCategory/view/frontend/templates

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $collection = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory')->create();//get current category
    $collection->addAttributeToFilter('level', array('eq' => 2));
    echo "<pre>";
    foreach ($collection as $category): ?>
<div><?php print_r($category->getData()) ?></div>
<?php   endforeach;
?>

Call this html in your cms page as: 

{{block class="Magento\Framework\View\Element\Template" template=Vendor_Name::categories.phtml}}

I hope this will help you. If still you have issue, let me know.

If this helps you, please accept it as solution and give kudos.

Regards.