简体   繁体   中英

Joomla : how to get the url of a specific Menu itemID?

Friends a newbie question.........I need help in getting the URL of a specific Menu itemID. The situation is like this:

I am running Joomla and asking for a user to input for a menu ID and choose a layout for that menu ID.

I want to do something else with this URL of the Menu itemID.

How can I get the URL of this Menu itemID provided by the user?

For Example if the user input is liek $this->get ('menulayoutid'>; and he inputs and ID of 54 then how do I get the URL for Menu ID 54.

Please note: I want to get this URL from within my PHP file and not in the browser so that I can use the value of that URL for some other purpose.

Kindly help.

$itemid = JRequest::getVar('Itemid');
$application = JFactory::getApplication();
$menu = $application->getMenu();
$item = $menu->getItem($itemid);
$link = new JURI($item->link);
$link->setVar('ItemId', $itemid);

Source: http://forum.joomla.org/viewtopic.php?p=1836005

However, we get the Itemid from anywhere (user input, from our own developed module using the "menu item" field type in the xml file as described in the Joomla Docs - Standard form field types )

// get the menuItemId from wherever...
// as described above or as in other posts here and do whatever with that!
$menuItemId = 'fromWherever'; // as an example "107";

// build the link to the menuItemId is just easy and simple
$url = JRoute::_('index.php?Itemid=' . $menuItemId);

i think if we need only a link to a specific menu id, this is the best solution, because we have absolutely less requests and a clean code

this works also in Joomla 3.0, 3.1

I just want to add that if you need to target a specific menu you pass the menu name as an argument to getMenu().

$itemid = JRequest::getVar('Itemid');
$application = JFactory::getApplication();
$menu = $application->getMenu( 'menu-name' );
$item = $menu->getItem($itemid);
$link = new JURI($item->link);
$link->setVar('ItemId', $itemid);

I'm not sure if Joomla changed the way this works since 2.5 or even 1.7 but I spent the worse half of 2 hours looking for this.

Hopefully it helps someone.

$menuID = $params->get('menuItem'); // from module field menu ex. '105'
$js = new JSite;
$menu = $js->getMenu();
$link = $menu->getItem($menuID)->route;

//Returns URL Friendly Link -> menu/article 
//Then format it -> 

$link = 'http://www.yoursite.com/index.php/'.$link;
echo '<a href="'.$link.'">Borrowed Menu Link Path</a>";

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, insert the following code where you wish to display the active menu item ID:

<?php 
$currentMenuId = JSite::getMenu()->getActive()->id;
echo $currentMenuId; 
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM