How to Check if a CCK Field Exists on a Node
There are times it's important to check to see if a CCK field exists, or has a value, on a node before trying to do sometihng with it.
Here's an example I used in node--blog.tpl.php to now show the thumbail if the Show Thumbnail custom field is set to No. The reason I had to do this was because I added this field after LOTS of nodes had already been created. I was getting 'index not defined' error messages on my old content because those nodes did not have a value for that field since the field didn't exist when the node was originally created.
<?php
$showthumb = "Yes"; //set default to Yes
$my_field_items = field_get_items('node', $node, 'field_show_thumb_on_post');
if ($my_field_items) {
$my_field_first_item = reset($my_field_items);
$my_field_value = $my_field_first_item['value'];
$showthumb = $my_field_value; //set it to whatever the node has it set to since we know this particular node has a value for the field
}
if ($showthumb == "Yes"){
print render($content['field_blog_image']) ;
}
?>
RELATED POST:
How to Print CCK Fields in Custom node Templates (node--custom.tpl.php)