Magento 2.2.2 how to check if current page is a CMS page in .phtml file?

by Wasiq Shahrukh   Last Updated January 12, 2018 13:09 PM

I am using the following code to check if the current page is Product detail page:

$objectManagerCms = \Magento\Framework\App\ObjectManager::getInstance();
$registry = $objectManagerCms->get('Magento\Framework\Registry');
$currentProduct = $registry->registry('current_product');
if ($currentProduct) {
    // Custom code

Now how to check if current page is a CMS page?

Answers 3

There are no registry variable add at cms page.

So, if you want to check a page is cms page then you need to check current controller and action name

Magento block has assign request object at block class ,So you can get current page full action name via



$currentFullAction = $block->getRequest()->getFullActionName();
$cmspages = array('cms_index_index','cms_page_view');
//CMS page
Amit Bera
Amit Bera
January 12, 2018 12:32 PM

Try to use below code in your .phtml file

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$request = $objectManager->get('\Magento\Framework\App\Request\Http');
$checkModule = $request->getModuleName();
if($checkModule == 'cms'){
    echo "cms page";
    echo "not cms page";
Chander Shekhar
Chander Shekhar
January 12, 2018 12:33 PM

The registry class could be used in many places. Thus, it's relatively weak check. I suggest you to check full action name of request, like so:

\Magento\Framework\App\Request\Http::getFullActionName() == 'catalog_product_view'
Andrey Konosov
Andrey Konosov
January 12, 2018 12:34 PM

Related Questions

Class instatiation with data

Updated November 20, 2017 11:09 AM

How to store value in object Magento 2

Updated February 17, 2017 12:09 PM

Magento 2 : How to use objectManager class object?

Updated October 24, 2016 09:03 AM

how pass parameter to object manager get

Updated December 25, 2016 08:03 AM