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 & Portfolio.
Can someone please guide me? I do not want to hard code the links as the item id would differ in production.
The default menu items fall under these main categories: Articles, Configuration Manager, Contacts, Newsfeeds, Search, Smart Search, System Links, Tags, User Manager, Weblinks, and Wrappers.
In Joomla, submenus can be shown either as one menu with two or more levels or as completely separate menu modules. This is useful for constructing menus showing parent and child level navigation or for showing sublevel menu items in entirely separate positions on your template.
Access your Joomla! 3 administrative dashboard and go to Menus > Main Menu > Add New Menu Item. Now you need to select the menu item type. In this example, we will show you how to make the drop-down menu link open a single Joomla!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With