Alex Rivera | Logout

Magento: How to tell if you're on a category page, or product page in .phtml file(s)

Asked 2010-08-17T15:00:54.053
22

I am trying to program into my .phtml files an if statement if the guest is on a category list page, or on a product page.

For example this code:

<?= Mage::app()->getFrontController()->getRequest()->getRouteName(); ?>

Returns "catalog" whenever l'm on a page other than a CMS page.

Is there a way l can use a similar method to know if the user is looking at a root category, sub category or an individual product page?

Any help would be greatly appreciated!

Edit
Report

1 Answer

3

I am afraid you are trying to do it the wrong way. I might be wrong, because you have not explained what is it exactly that you want to achieve, but I would use the layout xml to include your block on a product page with a parameter (say product-page="1") and similiarly on a category page (category-page="1").

Then you would be able to tell if you are on a product page or category page by examining those parameters inside your block:

if($this->getProductPage()) {
  //this is a product page, do some stuff
}
elseif($this->getCategoryPage()) {
  //this is a category page, do some stuff
}

Differentiating between main and subcategory pages might be more difficult, the first thing that comes to mind is analysis of the request variables, but that is certainly not the best approach.

answered 2010-08-17T17:26:36.090

Your Answer