Single level menu block for Drupal

Drupal’s menu system is really flexible and the menu blocks are versatile. Also there are a couple of 3rd party modules to display menus in all kinds of different ways.

There is a module to display a single level menu. But it will only display one menu — I needed two (main and sub).

So here is a quick PHP snippet to display a single level menu from Drupal:

Level 1 links only (no tree)

  1. $level = 0; /* zero-based */
  2. $lvl_menu = menu_navigation_links(variable_get('menu_primary_links_source', 'primary-links'), $level);
  3. if ($msm == variable_get('menu_primary_menu', 0)) {
  4. return theme('links', $lvl_menu, array('class' => 'menu') );
  5. }

If you don’t want the block to disapear when there is no menu, replace the return with print. I.e.

  1. $level = 0; /* zero-based */
  2. $lvl_menu = menu_navigation_links(variable_get('menu_primary_links_source', 'primary-links'), $level);
  3. if ($msm == variable_get('menu_primary_menu', 0)) {
  4. print theme('links', $lvl_menu, array('class' => 'menu') );
  5. }
  6. print "<br />";

UPDATE: see also the Menu block module.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You can use BBCode tags in the text.
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].

More information about formatting options