简体   繁体   English

Joomla:如何获取特定菜单itemID的网址?

[英]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. 朋友一个新手问题.........我需要帮助来获取特定菜单itemID的URL。 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. 我正在运行Joomla,并要求用户输入菜单ID并选择该菜单ID的布局。

I want to do something else with this URL of the Menu itemID. 我想使用菜单itemID的此URL做其他事情。

How can I get the URL of this Menu itemID provided by the user? 如何获得用户提供的此菜单itemID的URL?

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. 例如,如果用户输入是liek $this->get ('menulayoutid'>;并且他输入ID为54那么我如何获得菜单ID 54的URL。

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. 请注意:我想从我的PHP文件而不是浏览器中获取此URL,以便可以将该URL的值用于其他目的。

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 资料来源: 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 ) 但是,我们可以从任何地方获取Itemid(用户输入,使用Joomla Docs-标准表单字段类型中所述的xml文件中的“菜单项”字段类型,从我们自己开发的模块中获取)

// 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 我认为,如果我们只需要一个指向特定菜单ID的链接,那将是最好的解决方案,因为我们的请求绝对少而且代码干净

this works also in Joomla 3.0, 3.1 在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(). 我只想补充一点,如果您需要定位特定菜单,则可以将菜单名称作为参数传递给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. 我不确定Joomla是否从2.5甚至1.7开始就改变了它的工作方式,但是我花了2个小时中的更差的一半来寻找它。

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: 当您需要在Joomla中获取活动菜单项ID以便仅显示该菜单项的某些特定内容或仅显示菜单项的ID时,在希望显示活动菜单项ID的位置插入以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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