简体   繁体   English

如何获取基于名称的joomla菜单对象?

[英]how to get joomla menu object based on name?

This question is bit specific to joomla. 这个问题只针对joomla。

I know if with the code $menu = &JSite::getMenu() I can get the reference object of the complete menu. 我知道是否可以使用代码$menu = &JSite::getMenu()获得完整菜单的引用对象。 But how can i get a specif menu based on the name? 但是如何根据名称获取特定菜单?

My Scenario : I have footer-menu with items : home | 我的场景:我的页脚菜单包含以下项目:主页| about us | 关于我们| rules | 规则| privacy policy. 隐私政策。

I need to display links to two menu items Rules and privacy policy in a component. 我需要在组件中显示两个菜单项“规则”和“隐私策略”的链接。 I cannot hard code the links, as the itemid would be different in development and production environment. 我不能对链接进行硬编码,因为itemid在开发和生产环境中会有所不同。

Do we have some workaround like $menu = &JSite::getMenu()->get('footer-menu')->getMenuItem('rules'); 我们是否有一些解决方法,例如$menu = &JSite::getMenu()->get('footer-menu')->getMenuItem('rules'); which can give me refrence object to a particular menu item, from which I can create my links for that particular article. 这可以使我将对象指向特定菜单项,从中可以为该特定文章创建链接。

Thanks, Tanmay 谢谢,坦美

Method #1: 方法1:

$menu = & JSite::getMenu();
$item = $menu->getItems('link', 'index.php?option=com_content&view=article&id=1', true);

Method #2: 方法2:

$menu = & JSite::getMenu();
$item = $menu->getItems('alias', 'rules', true);

As far as I know, there isn't a built-in way to do this. 据我所知,没有内置的方法可以做到这一点。 But I feel your pain. 但是我感到你的痛苦。

Here's an adaptation of a function I built before. 这是我之前构建的功能的改编。 It's not recursive, so you'll only get one level deep in a menu hierarchy, but that was enough for me. 它不是递归的,所以您只会在菜单层次结构中深入一层,但这对我来说已经足够了。

function getMenuItems( $parentAlias ) {
    $db =& JFactory::getDBO();
    $sql = 'SELECT * FROM #__menu WHERE parent in ' .
           '(SELECT id FROM #__menu WHERE alias='.$db->Quote($parentAlias).') '.
           'AND published=1 ORDER BY ordering';
    $db->setQuery($sql);
    $results = $db->loadObjectList();
}

Let me know if this works for you. 让我知道这是否适合您。

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

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