PHP code to get active menu item ID in Joomla

When you need to get your active menu item ID in Joomla to display some specific content for only that menu item or just to show the ID of the menu item, you'll need to edit your index.php file in your templates folder (or use a third party plugin to add a PHP module somewhere in your site) and insert the following code where you wish to display the active menu item ID:

<?php 
 $currentMenuId = JSite::getMenu()->getActive()->id ;
echo "Your menu item ID is: $currentMenuId"; 
?>

And to display something only when your desired menu item is clicked (active), you can use:

<?php 
 $currentMenuId = JSite::getMenu()->getActive()->id ;
 if ($currentMenuId == "2") echo '<p>This is only visible with menu item which has ID 2.</p>'; 
?>

I used this last piece of code to add a http header refresh to a Joomla page so that when one certain menu item is clicked, the user automatically gets redirected after a period of time to another page. The code below needed to be inserted inside index.php <head></head> section:

<?php 
 $currentMenuId = JSite::getMenu()->getActive()->id ;
 if ($currentMenuId == "2") 
 echo '<meta http-equiv="refresh" content="63;url=http://www.theotherpageyouwanttoredirectto.com"> ';  ?>

Comments   

 
0 #4 zuber 2013-04-28 07:55
thank you\
Quote | Report to administrator
 
 
0 #3 Joomlaboy 2012-07-24 14:59
Hi Carlina,

Are you running an older version of Joomla? Because that's where I have tested it only and it works. Not sure if it'll work as expected on 2.5.
Quote | Report to administrator
 
 
0 #2 karlina 2012-07-17 08:56
Oh my and I don´t know how to insert the code snippet, obviusly so here it is:

d.pr/i/yOQZ









I tried to insert it to other positions too, I am a little clueless why it won´t show me the message...

Help would be grately appreciated...! Thanks karlina
Quote | Report to administrator
 
 
0 #1 karlina 2012-07-17 08:51
Hi there, it looks like exactly what I need,

I have tried this, but it won´t echo me the message, what could there be wrong?

If I want to show the message in the content area w3 I thought i post it here?:













I tried to insert it to other positions too, I am a little clueless why it won´t show me the message...

Help would be grately appreciated...! Thanks karlina
Quote | Report to administrator
 

Add comment


Security code
Refresh

Monetizing your blog is simple!

LikeLock - protect valuable links, trade them for a Like!Cover song contest and catalogue - Cover Toplist