如何从 joomla 获取特定的菜单项? [英] How to get specific menu items from joomla?

查看:22
本文介绍了如何从 joomla 获取特定的菜单项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题有点针对 Joomla.

This question is bit specific for Joomla.

我有一个主菜单,包括:

I have a main menu consisting of:

主页|关于我们|隐私政策|投资组合|联系我们.

每个菜单项都指向一篇文章.

Each menu item is link to an article.

现在在完整的站点上,组件和模块中有很多地方我需要显示两个链接:隐私政策和;文件夹.

Now on the complete site there are many places in the components and modules where I need to show two links : Privacy Policy & Portfolio.

有人可以指导我吗?我不想对链接进行硬编码,因为项目 ID 在生产中会有所不同.

Can someone please guide me? I do not want to hard code the links as the item id would differ in production.

推荐答案

有两种方法可以做到:

选项 1:

每次加载页面时,Joomla 都会加载菜单.您可以通过调用以下方法访问菜单.

Joomla loads menus every time page is loads. You can access the menus by calling the following methods.

// Get default menu - JMenu object, look at JMenu api docs
$menu = JFactory::getApplication()->getMenu();

// Get menu items - array with menu items
$items = $menu->getMenu();

// Look through the menu structure, once you understand it
// do a loop and find the link that you need.
var_dump($items);

这种方法更快,因为您不需要查询数据库.内存中的简单操作.

This method is faster because you don't need to query database. Simple operation in memory.

选项 2:

从数据库中获取.根据别名或其他内容从 jos_menu 获取菜单链接,或者通过文章别名从 jos_content 获取文章#,然后创建链接

Get it from the database. Either get menu link from jos_menu based on alias or something, or get article # from jos_content by article alias, then create the link

$db = JFactory::getDBO();

//  Load by menu alias
$query = "SELECT link FROM #__menu WHERE alias = 'privacy-policy'";
$db->setQuery($query);
$url = $db->loadResult();
$url = JRoute::_($url);


//  Load by article alias
$query = "SELECT id FROM #__content WHERE alias = 'privacy-policy'";
$db->setQuery($query);
$articleId = (int) $db->loadResult();
$url = JRoute::_("index.php?option=com_content&view=article&id=$articleId");

这篇关于如何从 joomla 获取特定的菜单项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆