如何从joomla获取特定菜单项? [英] How to get specific menu items from 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.
推荐答案
有2种方法可以实现:
选项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屋!