Printing a node field value in html.tpl.php template - Drupal 7

To print a value from a node field in your html.tpl.php template, add this code to your themes template.php file.

You will need to replace 'THEMENAME' with the machine name of your theme, and 'FIELD_NAME' with the machine name of your field (format usually field_my_field_name).

function THEMENAME_preprocess_html(&$vars) {
  $node = menu_get_object();
  if ($node && isset($node->nid)) {
    $node = node_load($node->nid);
    node_build_content($node);
    $vars['FIELD_NAME'] = render($node->content['FIELD_NAME']);
  }
}

To print out your field value in theme html.tpl.php template simply use the below:

<?php print $FIELD_NAME ?>

Another example

Here is an example with more realistic replacements:

function custom_preprocess_html(&$vars) {
  $node = menu_get_object();
  if ($node && isset($node->nid)) {
    $node = node_load($node->nid);
    node_build_content($node);
	$vars['title'] = check_plain($node->title);
    $vars['field_tags'] = render($node->content['field_tags']);
  }
}

Incidentally you should include some checks to see if the values are actually set otherwise you will get an error on pages where they aren't, eg:

<?php if ( isset($title) && isset($field_tags) ){
  print '<h2>' . $title .'</h2>' . $field_tags; 
​}
?>
Justin Chevallier

Justin Chevallier

Avid Drupal site builder & user for +9 years.

Comments

Submitted by Sean on

I am trying to get this working with a custom field I created called field_description which I have inserted into the description meta tag. In my template.php I have:

function rice_theme_preprocess_html(&$vars) {
$node = menu_get_object();
if ($node && isset($node->nid)) {
$node = node_load($node->nid);
node_build_content($node);
$vars['field_description'] = render($node->content['field_description']);
}
}

And then I have inserted this in my html.tpl.php in the correct area.

<meta name="description" content="<?php print $field_description ?>" />

When I push template.php and html.tpl.php to the server I get:

Notice: Undefined variable: field_description in include() (line 58 of /srv/bindings/1fe3326005e54fd3b99de58c9c39cc5b/code/sites/all/themes/rice_theme/html.tpl.php).

Any thoughts? Thanks.

Submitted by Justin on

The error you are getting suggests that page doesn't have the description field set. To get rid of the error you could should include it in your html.tpl.php with a check to only try include the description if it's actually set, for example:

<?php
    if (isset($field_description)){
       print '<meta name="description" content="' . $field_description . '" />';
    } 
?>

I've included this check to see if the value is set in my example now as I'm sure others will run into this problem.

Incidentally what you're going to find is that the value of your description will be included but it won't be any good as it is also going to include all the additional field html with it. I would suggest using the excellent 'Metatag' module for adding meta descriptions and that sort of thing.

If you still want to go this route you will probably need create some field templates in order to override and strip out the additional html.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.