Using taxonomy to give option of page layout - working but showing error (Notice: Undefined variable: node in include()) I have been building a site in Drupal 7. I have the content type "Basic Page" but added taxonomy terms for the user to be able to select "Standard" or "Full Width" page.

If the user selects "Full Width" the page.tpl.php file has some code which recognises the value chosen for the page removes a sidebar (usually seen on the "Standard" page).

It is working perfectly on the frontend but has an error in the backend.

Here is the code in the page.tpl.php page:

&lt;?php $field_page_width = field_view_field('node', $node, 'field_page_width'); ?&gt;

            &lt;?php if ($field_page_width['#items'][0]['taxonomy_term']-&gt;name == 'Standard') { ?&gt;         

                &lt;div class="col-xs-12 col-sm-12 col-md-8 col-lg-8 internal_content"&gt;

                    &lt;?php print render($page['content']); ?&gt;

                &lt;/div&gt;

                &lt;div class="hidden-xs hidden-sm hidden-md col-lg-1"&gt;&lt;/div&gt;

                &lt;div class="col-xs-12 col-sm-12 col-md-4 col-lg-3 side_content"&gt;

                    &lt;?php print render($page['side_content']); ?&gt;               

                &lt;/div&gt;

                &lt;div class="clearfix"&gt;&lt;/div&gt;

            &lt;?php } else { ?&gt;

                &lt;div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 internal_content"&gt;

                    &lt;?php print render($page['content']); ?&gt;

                &lt;/div&gt;

                &lt;div class="clearfix"&gt;&lt;/div&gt;

            &lt;?php } ?&gt;


I am receiving this error:


  Notice: Undefined variable: node in include() (line 76 of /home/public_html/sites/all/themes/zen/child_theme/templates/page.tpl.php).


Why do I get this error? Do I need to have something which establishes $node in the template.php file?
The page.tpl.php template file will receive a $node variable only if the user is visiting a node page; in other cases, it will not get any $node variable, even if the template file is still used (if a module doesn't suggest another template file that takes the precedence).
In fact, the documentation for page.tpl.php clearly describes the $node variable as follows. (Emphasis is mine.)


  $node: The node object, if there is an automatically-loaded node associated with the page, and the node ID is the second argument in the page's path (e.g. node/12345 and node/12345/revisions, but not comment/reply/12345).


Before accessing $node, you should be sure it has been set.

if (isset($node)) {
  $field_page_width = field_view_field('node', $node, 'field_page_width');
}


Also, field_view_field() is used to render a field. If you just need to get its value, you should use field_get_items().

if (isset($node)) {
  $items = field_get_items('node', $node, 'field_page_width');
}