I have a menu with a two level hierarchy. The menu has currently 8 items on the top level. I would like to display a separate banner for each of the 8 top level menu items. Example: One page banner for all nodes under "About Us", one page banner for all nodes assigned to "Contact", etc. Our website is http://www.qahc.org.au/

I think the best way to go about this (tell me if I'm wrong) is to add a PHP switch in my theme template.

I noticed that menu items have an ID just like nodes do, but they are allocated randomly, so I can't use them for my purpose easily. The top level menu item "About Us" is ID 45, the 2nd level menu item "Reports" that falls under "About Us" is ID 64, so there doesn't seem to be a logic connection (such as 7.0, 7.1, 7.2, 8.0, etc.).

What function can I use to find out which one of the 8 menu categories the currently displayed node belongs to, in order to choose and display the appropriate banner for that top level menu item?

switch ($toplevelmenuthatthisnodefallsunder) {
case "aboutus": echo "aboutus.gif"; break;
case "contact": echo "contact.gif"; break;
default: echo "genericbanner.gif"; } 

This website http://www.dvessel.com/node/87 seems to explain how to get the node id, but I need to reference against the top level menu for the displayed node. Any help much appreciated, thanks!

Comments

nevets’s picture

With this snippet you need to know the menu id's of the top level menu items (ex: "About Us"). It gets the active menu id, then gets the menu data and finally looks to see what the parent is.

$active = menu_get_active_item();

$item = menu_get_item($active);

switch ( $item->pid ) {  // Pid is the parent menu id
case 45:  // About us
   // Do something here
  break;

case 32:  // What ever?
  // Do something else
  break;
}
Anonymous’s picture

This is great! I got it to work with your help, thank you very much. I had to change $item->pid to $item['pid'] and I also needed to account for nodes which were deeper down, so I added a while loop.


   $toplevelids = array(0, 70, 77, 86, 103, 89, 45, 95);   
   $active = menu_get_active_item();
   $item = menu_get_item($active);
   
   while(!in_array($item['pid'], $toplevelids)) {
   $item = menu_get_item($item['pid']); }
     
   switch ($item['pid']) { // Pid is the parent menu id
   case 45: // About
   echo "about.gif"; break;
   case 95: // Contact
   echo "contact.gif"; break;
   case ...........
   default:
   echo "defaultbanner.gif";
   } 

Not sure if this is the most elegant way, but it works. If anyone thinks they can improve this code, please let me know. cheers!

Anonymous’s picture

The solution works only for nodes which are associated with a menu entry. However, our menu is limited to three levels. Does anyone have any good ideas how I can switch to the approprite page banner for nodes which do not have their own menu entry and thus do not return a menu ID?

nevets’s picture

There are any number of ways you can achieve this, it depends in part how your content is organized, in part how many images (compared to pages) and other factors.

One way would be to use a taxonomy vocabulary to associate the content with a term that reflects the image to be shown. You then could use the taxonomy image module and with a little code in page.tpl.php display an image based on a taxonomy term.

You could also use node type ($node->type) and map each node type to an image.

You could of course use the nid of the node, but if images are really this unique I would probably write a small module that allows me to associate a banner image with the node.

In general, you can map any information the node has to images, including but not limited to the author and the create date.

The bottom line, is the solution is tied to how you want to map the node to an image for the banner, what determines which image to use?