18

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.

peterh
  • 11,875
  • 18
  • 85
  • 108
jtanmay
  • 2,607
  • 6
  • 26
  • 36

9 Answers9

33

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");
Alex
  • 6,441
  • 2
  • 25
  • 26
16

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();
Don Gilbert
  • 740
  • 4
  • 11
  • Thanks Don! Seems it was worth checking my own question again! :) – jtanmay Aug 06 '12 at 22:11
  • glad it could help. I've used this a lot in my Joomla applications, and it took a while to find, so I figured I'd archive it somewhere! – Don Gilbert Aug 09 '12 at 20:35
  • Don Gilbert - is there a way to use this in 2.5 to get all menu items from all menus? I see that _items is protected. – betweenbrain Aug 08 '13 at 20:21
  • @DonGilbert (I know this is an old post) but wondered why you suggested going to the db, when jtanmay just asked, in effect to show the linked in another menu. I used your menu answer to solve another issue I had so thank you for posting – tristanbailey Jan 01 '14 at 11:56
  • 1
    @tristanbailey, I didn't suggest going to the DB, that was Alex's answer. Looking at it though, my answer isn't the best for the question. In fact it doesn't even answer the question, maybe it was edited? The actual easiest way is your answer below, using Menu Item Alias. – Don Gilbert Jan 23 '14 at 00:24
  • @DonGilbert ok, it is an old question and the answers on here are all useful but they do seems to solve lots of similar problems, so maybe the question was changed. I value following your work – tristanbailey Jan 28 '14 at 21:35
2

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.

roy712
  • 29
  • 1
  • 11
1

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.

tristanbailey
  • 4,427
  • 1
  • 26
  • 30
0

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?

Ahmad Alfy
  • 13,107
  • 6
  • 65
  • 99
0

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.

landed
  • 479
  • 6
  • 15
0

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
);
Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121
Gunjan Patel
  • 145
  • 6
0
<?php

$menuitemid = JRequest::getInt( 'Itemid' );
if ($menuitemid)
{
    $menu = JSite::getMenu();
    $menuparams = $menu->getParams( $menuitemid );
    $params->merge( $menuparams );
}

$propvalue= $params->get('property_name');

?>
Alex
  • 6,441
  • 2
  • 25
  • 26
ursitesion
  • 988
  • 2
  • 15
  • 26
  • this will not work because if you are in the component view the Itemid will be of the menu item that leads to the component. This is way off. – Alex Oct 11 '10 at 12:41
  • `JRequest` is deprecated. You need to use `JFactory::getApplication()->input` – n9iels Sep 13 '17 at 21:31
-1
$app = JFactory::getApplication();
$menu = $app->getMenu();
$items = $menu->getItems('menutype', 'mainmenu');
nomem
  • 1,568
  • 4
  • 17
  • 33
Ronald Joseph
  • 45
  • 1
  • 13