Drupal 7: Hiding labels with empty fields when viewing a node

WebMW picture WebMW · Aug 16, 2011 · Viewed 10.5k times · Source

How do I hide labels that have empty fields when viewing the actual node of a certain content type?

I'd really appreciate anyone's help, thanks for your time.

Answer

Jukebox picture Jukebox · Aug 16, 2011

Another way you could achieve this is by using a custom template file that would apply to all nodes of that content type.

Make sure that node.tpl.php exists in your sites/all/themes/[mytheme] directory first. This template must exist before other custom templates can be called.

Make a copy of your node.tpl.php and name it node--[contenttype].tpl.php (without the brackets).

If you have the Devel module enabled, you can throw a dpm($content); into the file to find out the name of the field you are trying to hide. Or you could look at the content type itself.

Once you have the name of the field, you can now insert this code before the print render($content); statement:

if (empty($content['my_field'])) {
  unset($content['my_field']);
}

Clear the cache, and your field will only appear if there is a value stored.