Hide a field label

Is there a way to hide the field label when rendering a custom field in Drupal 7?

In my node template I have:

<?php print render($content['field_image']); ?>

Which gives me:

<div class="field field-name-field-image field-type-image field-label-above">
  <div class="field-label">Image:&nbsp;</div>
  <div class="field-items">
    <div class="field-item even">
      <img typeof="foaf:Image" src="http://site.com/sites/default/files/image.jpg" alt="">
    </div>
  </div>
</div>

I'd like to not have the <div class="field-label">Image:&nbsp;</div>.

Is there a way to get rid of this other than in the CSS or manually creating the markup?

I've tried unset($content['field_image']['#title']), but that gives me <div class="field-label">:&nbsp;</div>.

Answers 5

  • Instead of hardcoding the setting for field labels in the template you should instead use the setting under Manage display tab where you add and remove fields for your entity.


  • To hide the name of a custom field (file field in this case with the name "file") in order to only show a list of downloadable files and not the name of the field do the following:

    • Choose the Content Type that contains the file field (I created a new type called "File List")
    • Navigate to the "Manage Fields" Tab
    • Under Label choose "Hidden" and for Format, "Generic File"
    • Save and create a new node of this content type and the name "file" should no longer be displayed



  • using the template field.tpl.php you can remove or customize the label ($label in the template) of your field. to use it you must add the template field field.tpl.php to your theme:

    <div class="<?php print $classes; ?>"<?php print $attributes; ?>>
      <?php if (!$label_hidden): ?>
        <div class="field-label"<?php print $title_attributes; ?>><?php print $label ?>:&nbsp;</div>
      <?php endif; ?>
      <div class="field-items"<?php print $content_attributes; ?>>
        <?php foreach ($items as $delta => $item): ?>
          <div class="field-item <?php print $delta % 2 ? 'odd' : 'even'; ?>"<?php print $item_attributes[$delta]; ?>><?php print render($item); ?></div>
        <?php endforeach; ?>
      </div>
    </div>
    

Related Questions