{"id":7233,"date":"2019-09-20T11:46:31","date_gmt":"2019-09-20T06:16:31","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=7233"},"modified":"2019-09-27T13:32:57","modified_gmt":"2019-09-27T08:02:57","slug":"magento-2-get-parent-category-specific-category","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-get-parent-category-specific-category\/","title":{"rendered":"Magento 2 &#8211; How to Get Parent Category of Specific Category"},"content":{"rendered":"<p>In this post I am going to explain how to get parent category 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 parent category object  *\/\r\npublic function getParentCategory($categoryId)\r\n{\r\n\treturn $this-&gt;getCategory($categoryId)-&gt;getParentCategory();\r\n}\r\n\r\n\/* Get parent category Id *\/\r\npublic function getParentId($categoryId)\r\n{\r\n\treturn $this-&gt;getCategory($categoryId)-&gt;getParentId();\r\n}\r\n\r\n\/* Get all parent categories ids  *\/\r\npublic function getParentIds($categoryId)\r\n{\r\n\treturn $this-&gt;getCategory($categoryId)-&gt;getParentIds();        \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 parent category object\r\n$parentCategoryObj = $block-&gt;getParentCategory($categoryId);\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($parentCategoryObj-&gt;getData());\r\necho \"&lt;\/pre&gt;\";\r\n \r\n\/\/ Get parent category Id\r\n$parentCategoryId = $block-&gt;getParentId($categoryId);\r\n\r\necho $parentCategoryId;\r\n \r\n\/\/ Get array of all parent category ids\r\n$parentIdsArray = $block-&gt;getParentIds($categoryId);\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($parentIdsArray);\r\necho \"&lt;\/pre&gt;\";<\/pre>\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 parent category object\r\n$parentCategoryObj = $category-&gt;getParentCategory($categoryId);\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($parentCategoryObj-&gt;getData());\r\necho \"&lt;\/pre&gt;\";\r\n \r\n\/\/ Get parent category Id\r\n$parentCategoryId = $category-&gt;getParentId($categoryId);\r\n\r\necho $parentCategoryId;\r\n \r\n\/\/ Get array of all parent category ids\r\n$parentIdsArray = $category-&gt;getParentIds($categoryId);\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($parentIdsArray);\r\necho \"&lt;\/pre&gt;\";<\/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 parent category 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-parent-category-specific-category\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":7330,"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\/7233"}],"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=7233"}],"version-history":[{"count":1,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7233\/revisions"}],"predecessor-version":[{"id":7234,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7233\/revisions\/7234"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/7330"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=7233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=7233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=7233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}