Show modules on all submenus of a menu item

(5 votes, average 4.40 out of 5)
This technique is available from MetaMod 2.4 onwards.

These are simple but extremely effective recipes. They allow you to specify particular menu ids (Itemids) and the module ids to show on them.

The specified modules will be shown on each of these menu items and any child menu items of these. The second example below shows modules only on the child menu items, not on the “parent”.

This is great for situations such as when you are constantly adding or removing child menu items under a parent menu. If each of the child items is supposed to inherit the module selections of the parent, it’s a pain to have to go into each of the modules to edit the menu assignments, each time you want to add a new menu item, just to add the new page you created.

Using this recipe, you don’t have to do this – just assign the MetaMod to “all pages”, set up the array of which menu items map to which modules, and it looks after itself.

To find the “Itemid” numbers to use, go the the “Menus” menu, select the menu that you want to target, and look at the menu hierarchy. The last column is labelled “ItemID” and contains the number that you need below.

Example:

  • You have a menu item called “Sports” with ItemID 27, and a number of sub-menus
  • You want to add modules 19 and 22 into the current module position for “Sports” and all of its sub-menus
  • Use: $mappings = array( 27=>19, 27=>22);

Example 2:

  • You have a menu item called “Sports” with ItemID 27 – you want module id 19 on it and all of its sub-menus
  • You also have a menu item called “Travel” with ItemID 34 – you want module id 22 on it and all of its sub-menus
  • Use: $mappings = array( 27=>19, 34=>22 );
/* SETUP:
 * $mappings maps the Itemid of the "parent" to the module
 * number that you want to show on that menu item or any
 * of its children.
 * Add as many as you like. Use a comma to separate each pair.
 * Itemid => module id
 */
$mappings = array(
  27=>19,
  34=>22
);
 
/* You should not need to edit anything under here */
$all_modules = array();
foreach ($mappings as $iid=>$mid) {
  if ( $menu_genius->check( "parent_item_ids = $iid" ) ) $all_modules[] = $mid;
}
 
return $all_modules;

Variant: show only on the submenus of a particular menu item, not on the “parent” menu item itself.

/* SETUP:
 * $mappings maps the Itemid of the "parent" menu item to the module
 * number that you want to show on all the children of that menu item.
 * Add as many as you like. Use a comma to separate each pair.
 * Itemid => module id
 */
$mappings = array(
  27=>19,
  34=>22
);
 
/* You should not need to edit anything under here */
$all_modules = array();
foreach ($mappings as $iid=>$mid) {
 if ( $menu_genius->check( "parent_item_ids = $iid" )
   and $Itemid != $iid ) $all_modules[] = $mid;
}
 
return $all_modules;

Variant 2: Show modules on all menu items on a complete menu (e.g. “mainmenu”, “usermenu” etc).

$menutype = $menu_genius->info("menutype");
if ($menutype == 'mainmenu') return WWW; 
if ($menutype == 'usermenu') return XXX; 
if ($menutype == 'menu3') return YYY; 
if ($menutype == 'menu4') return ZZZ; 
/* replace WWW-ZZZ with the module ids to display on those menus */