More than 800+ satisfied clients and counting, since 2010

Blog

Get Specific Joomla Menu Items in Easy Steps

Get Specific Joomla Menu Items

Get Specific Joomla Menu ItemsJoomla offers primary menu for navigation. You can also include secondary menu and custom menus in your Joomla template. So it is required to manipulate codes to get specific Joomla menu items. When it will be of use? Suppose you want to display only specific menu items at multiple places in your site. Ofcourse you can use custom menus but what if you require different menu items at different places? For such cases, the only solution is to get specific Joomla menu items.

How to Get Specific Joomla Menu Items?

This blog post will highlight the various possible ways by which you can add specific menu items.

Method 1: Call the following Joomla 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);

Method 2: Fetch it from the database

$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");

Method 3: Replace “main menu” with the menu type you want to fetch.

$app = JFactory::getApplication();
 $menu = $app->getMenu();
 $menu_items = $menu->getItems('menutype', 'mainmenu');

Any of these three methods will help you get specific Joomla menu items. If you still face problems, request a quote.

Written by

The author didnt add any Information to his profile yet