How to Identify Current Page in Magento 2 Phtml File

Simple. Isn’t it?
Any doubts can be mentioned in the Comments section below and I’d be happy to help!
Thanks.
Thank you for subscribing.
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.

Method to Identify Current Page in Magento 2 Phtml File

  1. That’s it.

  2. In such cases, you first have to identify the current page and apply conditions like if the current page is a home page then add a notification bar otherwise don’t display it. Or if the current page is a category page then display a percentage discount on the particular product.

As a part of such a business requirement and customizing the default features, the following solution to identify current page in Magento 2 phtml file can be helpful.
Create Test.php file at app/code/Vendor/Module/Block/
Magento 2 is the most preferred CMS among E-commerce store owners owing to its flexibility and customization ability. It allows fulfilling the business requirements to meet customer expectations or to improve the onsite user experience.
Something went wrong.
Never miss Magento tips, tricks, tutorials, and news.

Get Weekly Updates

For instance, if you want to add a custom notification bar on a particular page i.e., the Home page or CMS page, that notifies the current deals or festive discounts. Moreover, while working with any custom module, you may need to display the same header on every page instead of the CMS page. 

Use the below script to get current page in the template file.
Do share the solution with fellow developers via social media.

/**/

/*.mfp-bg{background:#000000d4}
/*]]>*/

/*.mfp-bg.mfp-ready{opacity:.6}
/*]]>*/

(based on 7 Reviews)