php Joomla:如何获取特定菜单项 ID 的 url?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4529567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:20:02  来源:igfitidea点击:

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

phpurlmenujoomla

提问by Vikram Rao

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

朋友一个新手问题.........我需要帮助获取特定菜单 itemID 的 URL。情况是这样的:

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.

我想用 Menu itemID 的这个 URL 做一些其他的事情。

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

如何获取用户提供的此 Menu itemID 的 URL?

For Example if the user input is liek $this->get ('menulayoutid'>;and he inputs and ID of 54then 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.

请帮忙。

回答by moinudin

$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

回答by Lahmizzar

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(用户输入,来自我们自己开发的模块,使用 xml 文件中的“菜单项”字段类型,如Joomla 文档 - 标准表单字段类型中所述

// 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

回答by Matt Grannary

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);

回答by Sparatan117

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>";

回答by Kathir

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; 
?>