Mitech Preloader
Magento2

Magento 2 – How to identify current page in phtml file?

Magento2 development

Here We will know, whether the current page is a CMS page or category view page or product view page or Homepage  in Magento 2

so the first step, to check current page is write the following script in the .phtml file, where you need to identify which page is this?

[php]

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$request = $objectManager->get(‘Magento\Framework\App\Action\Context’)->getRequest();

[/php]

Now you identify your current page by following code

[php]
if ($request->getFullActionName() == ‘catalog_product_view’) {
//you are on the product page
}
if ($request->getFullActionName() == ‘catalog_category_view’) {
//you are on the category page
}
if ($request->getFullActionName() == ‘cms_index_index’) {
//you are on the home page
}
if ($request->getFullActionName() == ‘cms_page_view’) {
//you are on the cms page
}
[/php]

That’s how you can identify current page in Magento 2
I hope it helps you.

In case that you have any queries or questions in above steps , You can do comment below!

blank