Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get specific menu items from joomla?

Tags:

joomla

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.

like image 779
jtanmay Avatar asked Sep 24 '10 13:09

jtanmay


People also ask

Which are different menu items in Joomla?

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.

Can we make sub menus in Joomla?

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.

How do I create a drop-down menu in Joomla?

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!


4 Answers

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");
like image 61
Alex Avatar answered Nov 01 '22 16:11

Alex


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();
like image 30
Don Gilbert Avatar answered Nov 01 '22 17:11

Don Gilbert


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.

like image 2
roy712 Avatar answered Nov 01 '22 17:11

roy712


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.

like image 1
tristanbailey Avatar answered Nov 01 '22 16:11

tristanbailey