How to check home page , category page , product page in Magento2

If you are using a template file i.e. .phtml then directly you can call on the page using below code:

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

$request = $objectManager->get('\Magento\Framework\App\Request\Http');

if ($request->getFullActionName() == 'cms_index_index') { //you are on the homepage }

if ($request->getFullActionName() == 'catalog_product_view') { //you are on the product page }

if ($request->getFullActionName() == 'catalog_category_view') { //you are on the category page }

and if you to get a current category and its data you can do it via

$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');

$category->getData('schbang_category_name');

where schbang_category_name is my custom category attribute

I hope this might help someone.

Comments