{"id":1627,"date":"2021-03-07T01:04:31","date_gmt":"2021-03-07T01:04:31","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/get-current-category-name-in-magento-2\/"},"modified":"2025-07-16T18:15:56","modified_gmt":"2025-07-16T12:45:56","slug":"get-current-category-name-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-current-category-name-in-magento-2\/","title":{"rendered":"How To Get Current Category Name in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2\u00a0CMS is superior to other CMSs when it comes to customization. The platform allows us to customize and develop functionalities to cater to modern business requirements. In order to do so, the developer may need to perform custom solutions as shown in this post.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, the store owner wants to offer discounts based on category. The store offers clothing as well as footwear but the discount is to be offered on the products from the footwear category only. To develop this functionality, one needs to get the category name of the landing page. In such a scenario, use the below solution to&nbsp;<em><strong>get current category name in Magento 2<\/strong><\/em>&nbsp;store.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Get Current Category Name in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Use the below code in the&nbsp;<strong>CategoryBrand.php&nbsp;<\/strong>file at&nbsp;<strong><strong>Vendor\\Module\\Block.<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Module\\Block;\n\nuse Magento\\Backend\\Block\\Template\\Context;\nuse Magento\\Framework\\Registry;\nuse Magento\\Framework\\View\\Element\\Template;\n\nclass CategoryBrand extends Template\n{\n\n    protected $registry;\n\n    public function __construct(Context $context, Registry $registry)\n    {\n        $this->registry = $registry;\n        parent::__construct($context);\n    }\n\n    public function getCurrentCategory()\n    {\n        return $this->registry->registry('current_category');\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Use the below code in&nbsp;<strong>module_index_index.xml&nbsp;<\/strong>at&nbsp;<strong><strong>Vendor\/Module\/view\/frontend\/layout.<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" layout=\"1column\"\n      xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\">\n    &lt;body>\n        &lt;referenceContainer name=\"content\">\n            &lt;block class=\"Vendor\\Module\\Block\\CategoryBrand\" name=\"module_index_index\"\n                   template=\"Vendor_Module::template_file.phtml\"\/>\n        &lt;\/referenceContainer>\n    &lt;\/body>\n&lt;\/page><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Add the below code in&nbsp;<strong>template_file.phtml&nbsp;<\/strong>at&nbsp;<strong>Vendor\/Module\/view\/frontend\/templates<\/strong>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n$currentCategory = $block->getCurrentCategory();\necho $currentCategory->getName() . '&lt;br \/>';\n?><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Simply, use the getCurrentCategory() in the file, wherever you need to get current category.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s all!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank You.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2\u00a0CMS is superior to other CMSs when it comes to customization. The platform allows us to customize and develop functionalities to cater to modern&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1627","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1627","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1627"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1627\/revisions"}],"predecessor-version":[{"id":14460,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1627\/revisions\/14460"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}