Magento: How to tell if you're on a category page, or product page in .phtml file(s)
It's been a while since I've dealt with frontend catalog pages, but give this a try.
Current versions of Magento register certain global variables (not PHP globals, but things global to the Magento system) on certain pages.
Calling the following
$category = Mage::registry('current_category');
$product = Mage::registry('current_product');
$product = Mage::registry('product');
will either return null if the objects haven't been set (i.e. you're on a page without a category or a product), or return category and product objects.
If a product object is returned you're on a product page.
If no product object is returned but a category object is, you're on a category page. Category objects have a method to getting the parent id
$category->getParentId()
A category without a parent id should be a top level category, categories with parent ids should be sub-categories.
That should give you what you need to identify where the current request is.
UPDATE: Coming back to this almost a decade later -- I likely would not rely on the contents of the registry alone to determine the page I'm on. Instead I'd use the full action name in combination with looking for the above the objects.
While Alan's answer will work, there is a more direct option, and you were actually on the right track with your code snippet... you just need to inspect the controller name rather than the module name:
<?php Mage::app()->getFrontController()->getRequest()->getControllerName(); ?>
That will return category
or product
based on their controllers being CategoryController.php
and ProductController.php
respectively.
This does assume that you've not installed any third-party modules that rewrite those controllers with their own.
I am not a big fan of checking if the current_category registry exists, because basically any controller could do this and it wouldn't necessarily mean it's a category. My way of doing it is a bit more robust:
$fullActionName = Mage::app()->getFrontController()->getAction()->getFullActionName();
if ($fullActionName == 'catalog_category_view') {
... //Category
}
elseif ($fullActionName == 'catalog_product_view') {
... //Product
}