Get Current URL in Magento and show something

jehzlau picture jehzlau · Aug 7, 2014 · Viewed 95k times · Source

I'm trying to get the current URL in Magento and show something if I'm currently on that page. So far, this is what I did and it worked.

 <?php
 $currentUrl = $this->helper('core/url')->getCurrentUrl();
 ?>     

 <?php if($currentUrl === 'http://powerplantv2.jehzlau.net/blog') { ?>I am in the blog page<?php } ?>

However, I don't want to hard code the URL in the source code, because If I transfer to another server, I need to modify the phtml file again.

I tried everything that I found online, but it didn't work. I hope some Magento expert here can enlighted me of what I'm doing wrong. :(

Answer

Axel picture Axel · Aug 8, 2014

You can retrieve the current URL path by doing the following:

$currentUrl = Mage::helper('core/url')->getCurrentUrl();
$url = Mage::getSingleton('core/url')->parseUrl($currentUrl);
$path = $url->getPath();

Then using some basic logic, you can target the /blog page.

$blogPaths = array('/blog', '/blog/', '/index.php/blog/');
if(in_array($path, $blogPaths))
{
    //Do something on /blog
}