SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH

Magento 2 – How to Identify Current Page Type in phtml File

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, checkout page, the home page, or CMS page. You can also customize or add the functionalities based on the current page.

Using below code snippet you can get current page type.

Add below code snippet in Block class.

<?php
namespace Magepow\CurentPageType\Block;
 
class Blockname extends \Magento\Framework\View\Element\Template
{
protected $context;
 
public function __construct(
\Magento\Framework\App\Action\Context $context,
array $data = []
)
{
$this-&gt;context = $context;
parent::__construct($context, $data);
}
 
public function getCurrentPage()
{
return $this->context->getFullActionName();
}
}

Add below code snippet in template file.

//Home Page
if ($block->getCurrentPage() == 'cms_index_index') {
// Do something on home page only
}
//CMS Page
if ($block->getCurrentPage() == 'cms_page_view') {
// Do something on CMS pages only
}
//Category View Page
if ($block->getCurrentPage() == 'catalog_category_view') {
// Do something on category view page only
}
//Product View Page
if ($block->getCurrentPage() == 'catalog_product_view') {
// Do something on product view page only
}

run commands

php -dmemory_limit=4G bin/magento setup:upgrade
php -dmemory_limit=4G bin/magento setup:static-content:deploy -f
php -dmemory_limit=4G bin/magento cache:flush

That’s it. Enjoy Magento 2!!

Hope this article will help you in some way, You can see useful articles in the next articles.

Anything you need support from Magento 2 feel free to contact us at Alothemes and

Phone: (+84)865633728

Email: support@alothemes.com