7

How do I get a field label by the field name in Drupal 7?

14

The label is set per instance (not per field) so you would use the field_info_instance() function with some appropriate arguments:

$entity_type = 'node';
$bundle_name = 'article';
$field_name = 'field_some_field';

$info = field_info_instance($entity_type, $field_name, $bundle_name);

$label = $info['label'];

There's a bunch of other useful functions in the Field Info API documentation that might be helpful in case you haven't seen it already.

2
  • Oops, sorry about that – Clive Jul 25 '12 at 20:27
  • 1
    That works for english. Doesn't seem to work when trying to get the translated version of a title. – doub1ejack Mar 3 '15 at 18:42
2

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? And passed a long time from the question to this answer...)

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.