This question is bit specific for Joomla.
I have a main menu consisting of:
Home|About US|Privacy Policy|Portfolio|Contacts US.
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 & P开发者_JAVA百科ortfolio.
Can someone please guide me? I do not want to hard code the links as the item id would differ in production.
There are 2 ways you can do it:
Option 1:
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.
Option 2:
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");
The easiest way to accomplish this in 2.5+ is:
$app = JFactory::getApplication();
$menu = $app->getMenu();
$menu_items = $menu->getItems('menutype', 'mainmenu');
Just replace 'mainmenu' with the menutype that you want to pull. This would equate to the system name for your menu, the same as you would select in the menu module.
Edit in response to @betweenbrain's question below: Get the menu object the same way as above, then:
// To get menu items filtered by access level of current user.
$filtered_menu_items = $menu->getItems(null, null);
// To get all menu items, unfiltered.
$all_menu_items = $menu->getMenu();
In Joomla there is an option to link with any menu with a particular hyperlink option. From backend menu structure where you put article link, from there you can selection other link as well.
The standard way to do it is here: http://docs.joomla.org/Help32:Menus_Menu_Item_Menu_Item_Alias
Just make a second menu with just Privacy Policy & Portfolio in and as a menu item type choose System Links > Menu Alias Item. Then you can choose to link it to the menu item from the menu you created already.
This way you will be able to change the original article link any time and all the alias will update.
<?php
$menuitemid = JRequest::getInt( 'Itemid' );
if ($menuitemid)
{
$menu = JSite::getMenu();
$menuparams = $menu->getParams( $menuitemid );
$params->merge( $menuparams );
}
$propvalue= $params->get('property_name');
?>
Isn't it easier to create a new menu containing two aliases to the menus you want then load them anywhere on the website using {loadposition}
or whatever?
I think you should create a new menu in joomla then make aliases type of menu items, you should do this to make sure you don't get duplicate content issues. I think Alex answer is ok if you want to do it with code but its harder to maintain and for someone to understand who comes along. As it is a menu item it doesn't belong in a component or module in my opinion.
To get all the menu items in joomla backend/administrator. Tested in Joomla 3.3+
<?php
// Create JApplicationSite instance to get all menu
$site = new JApplicationSite;
$menu = $site->getMenu();
// Get menu items - array filtered by access level of current user. Replace with `getMenu` to get all items or check @don-gilbert's answer.
$menuItems = $menu->getItems(null, null);
// Build please select option for no itemid
$selectOption = array();
$selectOption[] = JHTML::_(
'select.option',
'',
JText::_('COM_REDSHOP_PLEASE_SELECT'),
'id',
'title'
);
// Merge items to build select list.
$items = array_merge($selectOption, $menuItems);
// Just print array to understand the structure
echo "<pre>";
print_r($items);
echo "</pre>";
// Or create a select list directly using array.
echo JHtml::_(
'select.genericlist',
$items,
'menu_item_id',
'',
'id',
'title',
$this->detail->menu_item_id
);
$app = JFactory::getApplication();
$menu = $app->getMenu();
$items = $menu->getItems('menutype', 'mainmenu');
精彩评论