Use a Custom Field in page.tpl.php I have a custom field called site_category that I am using in a few different content types.  The field site_category is an entity reference to a content type which has only one field defined:  Title.  For the purposes of this example, I have three instances of that content:  Things, Widgets, Places

I have another content type called content that has a number of fields including site_category

(Think of this like taxonomy, but using content types rather than terms)

I would like to add in the site_category field to my page.tpl.php so that it not only displays the title of the page, it also displays the site category that it is related to.

So, as an example, if I had a "Super Duper Widget De-Duper" that fell under the widgets category...

example URI:  

.com/widgets/super-duper-widget-deduper


Page would render (styled of course):

Header
**Widgets**
**Super Duper Widget De-Duper**
Content
Footer


I have found a number of similar posts here on stackoverflow like this one:  Access custom node field in page.tpl.php  However, it dosen't work and I don't know what I am doing wrong.  There is a lot of php code provided, but I don't know where to put said php code because it is never really referenced.

I have added the output from the Devel module.  It appears that the field is being loaded as it is "there."  Shouldn't I be able to access it?



I have added the following code with a reference to the field that is displaying in Devel:

if (isset($variables['id'])) {
// Get the node ID and load it.
$n = node_load($variables['id']);

$variables['site_category'] = "Breakpoint 1";

//If "Breakpoint 1" shows up on the page output, we know that (isset($variables['id']))
//evaluates as  "true"

// Check if the node exists
if ($n) {
  $variables['site_category'] = "Breakpoint 2";

  //if "Breakpoint 2" shows up on the page output, we know that the ($n) evaluates 
  //evaluates as true.


I only added the two breakpoints - no other changes were made.  When I view the output, only Breakpoint 1 shows up meaning the node doesn't exist.
Since you want to function on a "page level" (outside the content area) what you can do is:


Use hook_preprocess_page
Get the node id being viewed
Query the node using node_load or just your "term field" alone using EntityFieldQuery


This would somehow look like:

Your theme or custom module

// This function is inside my template.php
function YOURTHEME_preprocess_page(&amp;$variables) {
  // This array key is only present when a page is a node page(node/&lt;nid&gt;).
  // If this is set, this contains the node object already.
  if (isset($variables['node'])) {
    $n = $variables['node'];

    // Don't forget to change this also to check if the reference field 
    // has at least one reference/target node.
    if (isset($n-&gt;body['und'][0])) {
      $variables['site_category'] = 'PARTY! This displays the field we need';
      /**
       * The above line is just an example.
       *
       * You would replace it with the codes to load the target node:
       *   $target_node = node_load($n-&gt;field_site_category[......]);
       *
       * Display the title of the target node
       *   $variables['site_category'] = $target_node-&gt;title;
       */
    }
  }
}


page.tpl.php

&lt;?php if (isset($site_category)): ?&gt;
  &lt;?php print $site_category; ?&gt;
&lt;?php endif; ?&gt;


I added some screenshots of the outputs:

When viewing a node page



When not viewing a node page



Also to get the array keys inside variables, there is a module called Devel. Install it like any module and use its helper function dsm($your_var). This displays the contents of the variable in a pleasant manner.