Drupal 7 CCK feilds deconstruction from $content in node.tpl.php file-Collection of common programming errors
After a long quest, finally I got the solution for splitting $content in Drupal 7 in node.tpl.php.
Previously i was using i was using :-
print $node->field_name['und'][0]['value'];
Which was giving a warning :-
Notice: Undefined offset: 0 in include() (line 24 of C:\xampp\htdocs\drup\sites\all\themes\myCustomTheme\node.tpl.php)
Now I am using the function below which works fine with no errors
$output= field_get_items('node', $node, 'field_name');
$output = $output[0]['safe_value'];
print $output;
Now the problem is I have more than 50 fields on the page , I don’t think it would be efficient to call field_get_items(‘node’, $node, ‘field_name’) function 50 times.
Whats the alternate ? OR should I strict to the field_get_items function OR should I strict to the print $node->field_name[‘und’][0][‘value’]; which I heard is not good to use in Drupal 7 as und is undefined. This though solves my problem but gives irritating warnings.
-
In your template you should have access to the variables that are preprocessed earlier.
Each of your field is actually a variable so you can do $field_name[LANGUAGE_NONE][0][‘safe_value’]
To get rid of error notices, I suggest that you use the isset() function
Originally posted 2013-11-09 19:12:06.