2

I added some fields to the "Article" content type. I want to output them in a page--front.tpl.php. How do I get the field label value?

When I use the following code in page--front.tpl.php, it doesn't show the name of the field label. Why? What code should I use to print the field label?

$node = node_load(1);
print_r($node);

The output is similar to the following:

    [field_sq] => Array(
      [zh-hans] => Array(
        [0] => Array(
          [value] => 250
        )
      )
    )
  • Just a note: when the tag you are using changes to "please-remove-this-tag," you are using a tag that you should not use; in this case, Drupal version tags are not allowed anymore. – kiamlaluno May 23 '11 at 10:14
3

In node-article.tpl.php add this code at the beginning

// loop through node properties
foreach ($node as $k => $v) {
  // ensure this property is a field
  if (substr($k,0,6) == 'field_') {
    // use the CCK function to get the field data for this field
    $fieldData = content_fields($k, $node->type);

    // add the label to the array
    $fieldLabels[$k] = $fieldData['widget']['label'];
  }
}

Than you can print labels like this:

<?php if ($node->field_YourField[0]['view']){ ?>

    <?php print $fieldLabels['field_YourField']; ?> // prints label (add div/span wrapper if you need it)

    <?php print $node->field_YourField[0]['view'] ?> // prints field value (add div/span wrpaer if you need it)

<?php } ?>

This is tested in Drupal 6. Hope it helps.

2

Fields are available in the node.tpl.php template (see the $content array), but not the page.tpl.php template unless you write some code to do that.

If you want to display node fields outside of the node template, Panels is another option but may be overkill depending on your needs.

  • if i want to use some code to output the label and the content how do i do? – enjoylife May 14 '11 at 14:03
  • Is there a reason why you can't display the fields in node.tpl.php? If there is, you'll need to write some code (either in a custom module or template.php) to get the label and content for the fields you need and make them available as variables in page.tpl.php. Look at preprocess functions to do this. – pushka May 14 '11 at 14:38
  • As node.tpl.php is always used for nodes, and fields are associated with nodes (among other entities), I don't see any good reason to output the field labels in page.tpl.php. Doing so would mean to duplicate code already present in Drupal. Is there a good reason to do so? – kiamlaluno May 14 '11 at 22:41
2

Try to use in your tpl.php the following code to print the fields:

<?php print_r(array_keys($content)); ?>

Then you see the values and you can go modify creating for example a field--yournamefield.tpl.php.

0

The easiest way to print the field label of a node is:

<?php print $content['field_your_field_name']['#title']; ?>

(I've discovered it after a lot of print_r arrays and objects of Drupal! If it's so easy, why isn't explained anywhere, or why is so hidden that I've not found the trick?)

0

You can use as follows to get field label

$entity_type = 'node'; 
$bundle_name = 'article'; 
$field_name = 'field_some_field'; 
$info = field_info_instance($entity_type, $field_name, $bundle_name); 
$label = $info['label']; 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.