{"id":12924,"date":"2021-07-03T11:17:07","date_gmt":"2021-07-03T05:47:07","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=12924"},"modified":"2021-10-08T10:34:11","modified_gmt":"2021-10-08T05:04:11","slug":"identify-current-page-type","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/identify-current-page-type\/","title":{"rendered":"Magento 2 &#8211; How to Identify Current Page Type in phtml File"},"content":{"rendered":"<p>In this post I am going to explain How to Identify Current Page Type in Magento 2.<\/p>\n<p>Sometimes when you are working on custom development in Magento 2 then, you need to know what the current type of page being viewed is.<\/p>\n<p>You can identify whether the current page is a category page, product page, checkout page, the home page, or CMS page. You can also customize or add the functionalities based on the current page.<\/p>\n<p>Using below code snippet you can get current page type.<\/p>\n<p>Add below code snippet in Block class.<\/p>\n<pre>&lt;?php\r\nnamespace &lt;Vendor&gt;\\&lt;Module&gt;\\Block;\r\n\r\nclass Blockname extends \\Magento\\Framework\\View\\Element\\Template\r\n{\r\nprotected $context;\r\n\r\npublic function __construct(\r\n\\Magento\\Framework\\App\\Action\\Context $context,\r\narray $data = []\r\n)\r\n{\r\n$this-&gt;context = $context;\r\nparent::__construct($context, $data);\r\n}\r\n\r\npublic function getCurrentPage()\r\n{\r\nreturn $this-&gt;context-&gt;getFullActionName();\r\n}\r\n}\r\n<\/pre>\n<p>Add below code snippet in template file.<\/p>\n<pre>\/\/Home Page\r\nif ($block-&gt;getCurrentPage() == 'cms_index_index') {\r\n\/\/ Do something on home page only\r\n}\r\n\/\/CMS Page\r\nif ($block-&gt;getCurrentPage() == 'cms_page_view') {\r\n\/\/ Do something on CMS pages only\r\n}\r\n\/\/Category View Page\r\nif ($block-&gt;getCurrentPage() == 'catalog_category_view') {\r\n\/\/ Do something on category view page only\r\n}\r\n\/\/Product View Page\r\nif ($block-&gt;getCurrentPage() == 'catalog_product_view') {\r\n\/\/ Do something on product view page only\r\n}\r\n<\/pre>\n<p>That&#8217;s it. Enjoy Magento 2!!<\/p>\n<p>Check this <a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/set-default-values-system-configuration-fields\/\">Best Magento 2 Tutorial<\/a>.<\/p>\n<p>Thank You.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain How to Identify Current Page Type in Magento 2. Sometimes when you are working on custom development in Magento 2 then, you need to know what the current type of page being viewed is. You can identify whether the current page is a category page, product page,&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/identify-current-page-type\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":10,"featured_media":13182,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[729,9,10,2376,2153],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/12924"}],"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\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=12924"}],"version-history":[{"count":3,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/12924\/revisions"}],"predecessor-version":[{"id":13181,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/12924\/revisions\/13181"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/13182"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=12924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=12924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=12924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}