1

So I have a website structure as follows: Homepage > Level 1 > Level 2 > Level 3 (Article)

On display of a Level 1 page I want it to print out all of the level 2 articles that are a child of that content (relationships taken from the main menu). Information that needs to be printed is title, body summary, plus a custom image field that I have added.

This image shows the desired effect: the user is in About Us (Level 1), and so the main body of the site displays teasers of the level 2 pages (Who we are, what we do etc.) that have been set as children to About Us in the main menu.

example of functionality

Is it possible to take the parent/child relationships from the main menu, then on a page print the content of the child pages in this way?

Or, if not using the menu, is there a more appropriate/logical way to do this in Drupal?

I have lots of experience in using MySQL/PHP and writing custom modules in other content management systems however new to Drupal. Thank you for your help!

0

2 Answers 2

1

I managed to do this in the following way:

function theme_get_page_children($nid) {
         $parentPath = 'node/'.$nid;
          $parentId = db_query("SELECT mlid FROM {menu_links} WHERE link_path ='".$parentPath."'");

      foreach($parentId as $pid){
         $finalID = $pid->mlid;
      }

        $getChildren = db_query("SELECT link_path FROM {menu_links} WHERE plid='".$finalID."' ORDER BY weight");

      foreach($getChildren as $child){
          $nodeID = str_replace('node/', '', $child->link_path);
          $children[]= $nodeID;
      }

        if(isset($children)){
             foreach($children as $nid){
                 $node = node_load($nid);
                 if($node->status ==1){ //check node is published
                      print render(node_view($node, $view_mode = 'teaser'));
                 }
            }
        }
    }

And on the template files where to call it:

innovista_get_page_children($node->nid)

I think using foreach to get $finalID was bad practice. I also think I should have used drupals menu API to call the child links instead of querying the database. So if anyone knows how to improve this in these ways, or tidy up the code in general then comment below :)

0
1

I've did it by adding a new variable for the menu to the page template in template.php with the theme_preprocess_page function:

function YOUR-THEME_preprocess_page(&$variables){

  // new theme variables for  child page menu

  // get the main menu variable
  $pid = variable_get('links__system_main_menu', 'main-menu');

  // get the main menu tree
  $main_menu_tree = menu_tree_page_data($pid);  

  // iterate over the active menu and add each submenu item 
  foreach ($main_menu_tree as $item) {
      if ($item['link']['in_active_trail']) {
            $submenu = $item['below'];
        }
  }

  // create new page variable for the menu
  if (!empty($submenu)){
      $variables['page_nav'] = menu_tree_output($submenu);
  }
}

After this clean template cache and add the following to your page template, im my case, page.tpl.php:

<?php print drupal_render($page_nav); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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