Determining which Magento CMS page you are on

This post describes how to achieve what is effectively the same as the WordPress is_page() method in Magento. Surprisingly I couldn’t turn up much on Google about this, so thought it was worth a blog post on how I did it.

I’m using a modified version of the jQuery backstretch plugin (posted here). I wanted the main product image to be shown as the background on each product page, but on CMS pages I wanted specific images to be displayed as the background for each page.

The issue is that the Javscript for the backstretch is included in the head, so you need to be able to establish in templates/page/html/head.pthml whether you are on a product page or a CMS page, and if you’re on a CMS page, which CMS page you are on.

Here’s how you do it;

<?php 
// We check to see if we are on a single product page or not.  If we are, we set the main image as the background image. 
$onCatalog = false;
if(Mage::registry('current_product')) {
	
	// We're on a product page
    $onCatalog = true;
	$backurl =  Mage::helper('catalog/image')->init(Mage::registry('current_product'), 'image')->resize(1355,720);

} else {

	// We are not on a product page.  We check to see that we are on a CMS page.

	if (Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms') {
		
		// We are on a CMS page. Depending on which one we're on we switch the background image.
		
		switch (Mage::getSingleton('cms/page')->getIdentifier()) {
		
			case "home":
				$backurl = "http://www.thesite.com/media/mainback1.jpg";
				break;
			case "about-us":
				$backurl = "http://www.thesite.com/media/mainback2.jpg";
				break;			
			case "contact-us":
				$backurl = "http://www.thesite.com/media/mainback3.jpg";
				break;	
		        default:
				$backurl = "http://www.thesite.com/media/mainback5.jpg";
		}		
	
	}	

}
?>

	<script type="text/javascript">		
		var $j = jQuery.noConflict();
		$j(document).ready(function()
		{
		
		$j.backstretch( "<?php echo $backurl;?>", {speed: 500} ).delay(1500);
			
		});
	</script>