2

Let's say I have the following menu:

  • Smartphones
    • Android
      • LG Nexus 4
      • Samsung Galaxy Note 2
      • HTC One
      • Motorola Droid RAZR MAXX HD
    • iOS
      • iPhone 4
      • iPhone 4S
      • iPhone 5
    • Windows Phone
      • HTC Windows Phone 8X
      • HTC Windows Phone 8S
      • Nokia Lumia 920
      • Nokia Lumia 820

I want a menu that will display all the third level items if I'm on the parent node. So, if I'm on the Android page, I should see LG Nexus 4, Samsung Galaxy Note 2, HTC One, and Motorola Droid RAZR MAXX HD.

The only recommendation I keep seeing is to use the Menu block module, but I want to just create the menu myself in PHP. How?

  • 1
    Have you checked out Cute Menus or Accordion Menus modules? – longboardnode Mar 25 '13 at 11:44
  • @longboardnode No I haven't. I do not want to use a module. – cowgod Mar 25 '13 at 20:33
  • ok, can you clarify what exactly you are trying to achieve or a sample menu strucutre of what you want? – longboardnode Mar 26 '13 at 21:16
1

I figured it out. In template.php, I added the following function:

function MYTHEME_preprocess_page(&$variables) {
    $active_tree = menu_get_active_trail();
    $parent = array_pop($active_tree);
    if($parent['depth'] == 3) {
        $parent = array_pop($active_tree);
    }

    $parameters = array(
        'active_trail' => array($parent['plid']),
        'only_active_trail' => FALSE,
        'min_depth' => 3,
        'max_depth' => 3,
        'conditions' => array('plid' => $parent['mlid']),
      );

    $tertiary_menu = menu_build_tree($parent['menu_name'], $parameters);

    $links = array();
    foreach($tertiary_menu as $branch=>$link) {
        array_push($links, $link['link']);
    }

    $tertiary_links_vars = array(
        'links' => $links,
        'attributes' => array('class' => array('sub-nav')),
        'heading' => array(
            'text' => t('Tertiary menu'),
            'level' => 'h2',
            'class' => array('element-invisible'),
        ),
    );

    $variables['tertiary_menu'] = theme_links($tertiary_links_vars);
}

In my page.tpl.php file, I added the following:

<?php print $tertiary_menu; ?>

1

I follow cowgod in his solution, although I propose a slight tweak which allows a more flexibel rendering of the menu in the template:

function MYTHEME_preprocess_page(&$variables) {
    $active_tree = menu_get_active_trail();
    $parent = array_pop($active_tree);
    if($parent['depth'] == 3) {
        $parent = array_pop($active_tree);
    }

    $parameters = array(
        'active_trail' => array($parent['plid']),
        'only_active_trail' => FALSE,
        'min_depth' => 3,
        'max_depth' => 3,
        'conditions' => array('plid' => $parent['mlid']),
      );

    $tertiary_menu = menu_build_tree($parent['menu_name'], $parameters);

    $links = array();
    foreach($tertiary_menu as $branch=>$link) {
        array_push($links, $link['link']);
    }

    $variables['tertiary_menu'] = $links;
}

In page.tpl.php , you can then add:

<?php 
    print theme('links', array(
      'links' => $tertiary_menu,
      'attributes' => array('class' => array('sub-nav')),
      'heading' => array(
        'text' => t('Tertiary menu'),
        'level' => 'h2',
        'class' => array('element-invisible'),
      ),
    ); 
?>

This way, you'll have a more flexible solution.

  • Hi Nils and welcome. This website has a Q&A model, not a traditional forum. There is only one good answer, not a thread of suggestions. Please update your answer to include all the details so it becomes a complete answer to the question. – Neograph734 Apr 23 '16 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.