{"id":7235,"date":"2019-09-20T11:49:07","date_gmt":"2019-09-20T06:19:07","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=7235"},"modified":"2019-09-27T13:35:07","modified_gmt":"2019-09-27T08:05:07","slug":"magento-2-get-sub-categories-specific-category","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-get-sub-categories-specific-category\/","title":{"rendered":"Magento 2 &#8211; How to Get Sub Categories of Specific Category"},"content":{"rendered":"<p>In this post I am going to explain how to get sub categories of specific category in magento 2.<\/p>\n<p><strong>1. Using Dependency Injection<\/strong><\/p>\n<p>Add below code snippet in Block class.<\/p>\n<pre class=\"lang:default decode:true \">protected $_categoryFactory;\r\n        \r\npublic function __construct(\r\n\t\\Magento\\Backend\\Block\\Template\\Context $context,        \r\n\t\\Magento\\Catalog\\Model\\CategoryFactory $categoryFactory,\r\n\tarray $data = []\r\n)\r\n{\r\n\t$this-&gt;_categoryFactory = $categoryFactory;        \r\n\tparent::__construct($context, $data);\r\n}\r\n\r\n\/* Get category object *\/\r\npublic function getCategory($categoryId)\r\n{\r\n\t$category = $this-&gt;_categoryFactory-&gt;create()-&gt;load($categoryId);\r\n\treturn $category;\r\n}\r\n\r\n\/* Get all children categories IDs *\/\r\npublic function getAllChildren($asArray = false, $categoryId)\r\n{\r\n\treturn $this-&gt;getCategory($categoryId)-&gt;getAllChildren($asArray);\r\n}\r\n\r\n\/* Get children ids comma separated *\/\r\npublic function getChildren($categoryId)\r\n{\r\n\treturn $this-&gt;getCategory($categoryId)-&gt;getChildren();\r\n}<\/pre>\n<p>Add below code snippet in template file.<\/p>\n<pre class=\"lang:default decode:true \">$categoryId = 10;\r\n \r\n\/\/ Load category by category ID\r\n$category = $block-&gt;getCategory($categoryId);\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($category-&gt;getData());\r\necho \"&lt;\/pre&gt;\";\r\n\r\n\/\/ Get comma-separated children category ids\r\n$childrenCategoryIds = $block-&gt;getChildren($categoryId);\r\n\r\necho $childrenCategoryIds;\r\n\r\n\/\/Get all children category ids as an array\r\n$allChildrenCategoryIdsArr = $block-&gt;getAllChildren(true, $categoryId);\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($allChildrenCategoryIdsArr);\r\necho \"&lt;\/pre&gt;\";\r\n\r\n\/\/Get all children category ids ids comma-separated\r\n$allChildrenCategoryIds = $block-&gt;getAllChildren(false, $categoryId);\r\n\r\necho $allChildrenCategoryIds;<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>2. Using Object Manager<\/strong><\/p>\n<pre class=\"lang:default decode:true \">$objectManager =  \\Magento\\Framework\\App\\ObjectManager::getInstance();        \r\n\r\n$categoryFactory = $objectManager-&gt;get('\\Magento\\Catalog\\Model\\CategoryFactory');\r\n \r\n$categoryId = 10;\r\n\r\n\/\/ Load category by category ID\r\n$category = $categoryFactory-&gt;create()-&gt;load($categoryId);\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($category-&gt;getData());\r\necho \"&lt;\/pre&gt;\";\r\n\r\n\/\/ Get comma-separated children category ids\r\n$childrenCategoryIds = $category-&gt;getChildren($categoryId);\r\n\r\necho $childrenCategoryIds;\r\n\r\n\/\/Get all children category ids as an array\r\n$allChildrenCategoryIdsArr = $category-&gt;getAllChildren(true);\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($allChildrenCategoryIdsArr);\r\necho \"&lt;\/pre&gt;\";\r\n\r\n\/\/Get all children category ids ids comma-separated\r\n$allChildrenCategoryIds = $category-&gt;getAllChildren(false);\r\n\r\necho $allChildrenCategoryIds;<\/pre>\n<p>Thats it. Enjoy Magento 2!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain how to get sub categories of specific category in magento 2. 1. Using Dependency Injection Add below code snippet in Block class. protected $_categoryFactory; public function __construct( \\Magento\\Backend\\Block\\Template\\Context $context, \\Magento\\Catalog\\Model\\CategoryFactory $categoryFactory, array $data = [] ) { $this-&gt;_categoryFactory = $categoryFactory; parent::__construct($context, $data); } \/* Get category object&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-get-sub-categories-specific-category\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":7331,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[10,27],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7235"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=7235"}],"version-history":[{"count":1,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7235\/revisions"}],"predecessor-version":[{"id":7236,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7235\/revisions\/7236"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/7331"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=7235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=7235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=7235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}