Community Documentation

Show title of parent node in node heirarchy

Last updated February 13, 2010. Created by wrb123 on November 14, 2008.
Edited by willieseabrook, paganwinter. Log in to edit this page.

here is the code to solve this:

Hierarchy:
TypeA
-TypeB (has nodereference field typeb_to_typea) this field shows the name of node a
--TypeC (has nodereference field typec_to_typeb) this field shows the name of node b

install Computed Field module, make a computed field in content type typeC, and put this in it:

<?php
$mytempvar
=db_result(db_query("SELECT
field_typeb_to_typea_nid FROM content_type_typeb WHERE
nid=%d"
,$node->field_typec_to_typeb[0][nid]));

$node_field[0]['value']=db_result(db_query("SELECT title FROM node WHERE nid=%d", $mytempvar));
?>

(returns node Title) Note: look at Hierarchy above and depending on what fields you use in your content types and what their names are, you will have to change
field_typeb_to_typea_nid
to
field_your_field_name_nid (attach the _nid to whatever the name of your field is, your field should start with field_ no matter what)
also
content_type_typeb
to
content_type_yourtype (content_type_ + whatever your type is)
also
field_typec_to_typeb
to
field_your_field_name (your field should start with field_ )

if you want to return the node Number/ID instead, do this (same field name rules apply to this as the code above):

<?php
$node_field
[0]['value'] = db_result(db_query("SELECT
field_typeb_to_typea_nid FROM content_type_typeb WHERE
nid=%d"
,$node->field_typec_to_typeb[0][nid]));
?>

(returns node ID #)

About this page

Drupal version
Drupal 6.x

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.