anyone know how to find the node id of the parent of a particular node is?

i've found some talk of doing this with a taxonomy set up, but i've not been using taxonomy stuff.

i just need to output the title of the section a particular article sits under.

any thoughts?

Comments

nevets’s picture

By default node do not have parents. Book nodes may have a parent. There are modules to define relations ships. And CCK fields to allow node references. In you case since you are not using taxonomy how do you know what the section the node is under?

leemarrett’s picture

This is a great answer -- thank you! (my knowledge of drupal is slowly increasing...)

The hierarchy exists only through the menu structure. Each page is accessed from the menu. So in my menu if I have:

Apples
> Gala
> Royal

Oranges
> Dimply
> Zesty

And I'm on the Royal page, I know its parent is Apples.

Is there a way of accessing the menu's structure from within a node? What I really want to do is:

Say I'm on the Dimply page. There's a "Related reading" sidebar that has a link to the "Gala" page. Under that link, it has a bit of text that says "Found in: Apples"

So I need to find out where Gala sits in releation to the menu and print out what its parent is.

nevets’s picture

You do not say where you are trying to print the information. This version wiill work from node.tpl.php (a good place to do it) or anywhere you have a valid $node.

<?php 
$item = menu_get_item(NULL, "node/$node->nid");
if ( $item['pid'] ) {
  $parent = menu_get_item($item['pid']);
  print $parent['title'];
}
?>
leemarrett’s picture

that's fantastic!

i'm trying to print the information from within node.tpl.php, or more specific versions thereof (node-page.tpl.php etc).

your example works perfectly for pages a few levels down the menu, but for pages at the top level you get "Main Navigation".

I'm sure from here I can just set up a condition to print the current level if the parent is "Main Navigation", for example if I was on the Apples page, it tells me it's found in Apples not "Main Navigation"

nevets’s picture

Try

<?php
$item = menu_get_item(NULL, "node/$node->nid");
if ( $item['pid'] ) {
  $parent = menu_get_item($item['pid']);
  if ( $parent['pid'] ) {
    $page_title = $parent['title'];
  }
   else {
      $page_title = $item['title'];
   }
   print $page_title;
}
?>
leemarrett’s picture

wow! thanks so much that works really well :) perfectly in fact.

leemarrett’s picture

any suggestions as to how to get the url of the parent as well?

or would $parent['path'] do the trick?

nevets’s picture

To get a url you would want something like

print l($parent['title'] , $parent['path'] );
leemarrett’s picture

hooray!

thanks so much :)

ak’s picture

Hi there, has someone got this working for Drupal 6.x. The "NULL" param isn't necessary anymore, for the function menu_get_item, but my $item array is missing 'pid'? Although my node is definitely in the second level of my primary menu.

mooffie’s picture

Use menu_get_active_trail(). Do print_r to inspect the returned value.

(In D6/7 there are "menu routers" (url handling) and "menu links" (items in menus). You're after menu links, but menu_get_item() deals with menu routers, so it doesn't return 'pid' which is relevant only for "menu links".)

ak’s picture

Hi, I followed your suggestion but that returned array still hasn't gave me any information or reference to the parent item in the menu system or anything else useful, no pid, mid, menu, link, nothing …?

I copied following code, found here http://drupal.org/node/480810#comment-1664160 in my page.tpl.php which doesn't output anything to me? Still no success!

<?php $myactive_trail = menu_get_active_trail(); ?> 
<div class="mymenuparent">
  <a href="<?php print ($myactive_trail['1']['link_path']); ?>"><?php print ($myactive_trail['1']['link_title']); ?></a>
</div>

Coud you provide me with some more information on how I could handle my task or why this isn't working for me? Documentation is pretty rare about this. Thanks in advice.

tlarr’s picture

Drupal 6 you will need to change

$item = menu_get_item(NULL, "node/$node->nid");

to

$item = menu_get_item("node/$node->nid");

Guide
http://api.drupal.org/api/drupal/includes%21menu.inc/function/menu_get_i...

janekD7’s picture

For drupal 7 I found this answer useful:
http://stackoverflow.com/questions/7053828/how-to-display-parent-menu-it...

<?php
  $menuParent = menu_get_active_trail();
  //get rid of the last item in the array as it is the current page
  $menuParentPop = array_pop($menuParent);
  //Just grab the last item in the array now
  $menuParent = end($menuParent);
  //if it is not the home page and it is not an empty array
  if(!empty($menuParent) && $menuParent['link_path'] != ''){
    print $menuParent['title'];
  } else{
    print $title;
  }
?>