Magento: How can i do a if product page then, else if category page then, else if cms page then

Magento: How can i do a if product page then, else if category page then, else if cms page then

August 11, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: How can i do a if product page then, else if category page then, else if cms page then

Magento: Conditional Logic for Breadcrumb Titles Based on Page Type

As a developer working with Magento, you may find yourself needing to display different breadcrumb titles depending on the type of page being viewed—whether it’s a CMS page, category page, or product page. In this post, we’ll walk through how to achieve this using conditional logic in your PHTML files and Magento’s built-in registry.

Understanding the Challenge

The core challenge lies in identifying the current page type within your template file (in this case, breadcrumbs.phtml). You’re already aware that you can access the product and category objects via the Mage::registry method. The question is: how do we determine which object to use based on the page being viewed?

Identifying the Current Page Type

In Magento, the Mage_Core_Model_Observer_Abstract class handles various events, including page load. When a specific type of page (like CMS, category, or product) loads, it is registered as an event observer.

To identify which type of page is currently being viewed, we can check for the existence of these objects in the registry:

$current_product = Mage::registry('current_product');
$current_category = Mage::registry('current_category');

However, simply checking for their presence won’t help us create a conditional statement. We need to ensure that the appropriate object is available before attempting to access its properties.

Building the Conditional Statement

To solve this, we can use PHP’s isset function in combination with an if-elseif-else structure:

<?php if (isset($current_product)): ?>
    <h1><?php echo $current_product->getName(); ?></h1>
<?php elseif (isset($current_category)): ?>
    <h1><?php echo $current_category->getName(); ?></h1>
<?php else: ?>
    <!-- If neither is set, assume it's a CMS page -->
    <h1>Home</h1>
<?php endif; ?>

In this code snippet:

  • The isset function checks if the variable exists and isn’t null.
  • The if-elseif-else structure evaluates each condition sequentially.
  • If $current_product is set, it displays the product name as the <h1> title. If not,

it proceeds to check for a category.

  • If neither $current_product nor $current_category are set, we assume it’s a CMS page and display a default ‘Home’ title. This catch-all ensures that no PHP errors occur if an unexpected type of page is loaded.

Conclusion

Conditional logic in Magento templates allows you to create dynamic content based on the specific context of each page. By utilizing built-in registry objects and checking for their existence, you can implement robust, error-resistant code that adapts seamlessly to different page types.

Remember to always test your modifications thoroughly in a development environment before deploying them to live sites. For more advanced Magento customization techniques and best practices, be sure to explore the Laravel Company blog.

Happy coding!

Catalog Extensions Themes

Our network