Magento 2 get parent categories We can use dependency injection or object manager to get all parent categories. Here in this article we are going to create one example in which we will get all parent categories using object manager.
Magento 2 get parent categories Example
Using object Manager You can get all parent categories simply below-
Magento 2 get parent categories Example:
$categoryId = 5; $_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $category = $_objectManager->create('Magento\Catalog\Model\Category')->load($categoryId); foreach ($category->getParentCategories() as $parent) { echo $parent->getId(); } |
The above example will return all parent categories.