For most people, the following method is generally used to print the value of a field.
$ Node-> field_name ['und '] [0] ['safe _ value']
This is a general practice, but not a general practice.
The above practice has the following problems:
1: The use of 'und 'may cause problems in multiple languages.
2: In the above method, the original field value is printed; it is not a normal field rendering method.
3: [0] ['safe _ value']. This method only renders the first value. If it is a multi-value field, data will not be rendered.
4: for some fields, such as entityreference fields, there is no ['safe _ value']. This method is prone to syntax errors.
We recommend that you use Drupal native to render fields:
Field_view_field
The general usage is as follows:
$ Output = field_view_field ('node', $ node, 'Field _ name ');
Print render ($ output );
This method can be compatible with the above-mentioned problems. At the same time, you can render your fields more flexibly.
For example, for image fields, the default label is hidden and the image is displayed using medium's imagestyle. The following code can be used:
$ Node = node_load (NID );
$ Display = array (
'Label' => 'ddn ',
'Settings' => array (
'Image _ style' => 'food _ pairing ',
)
);
$ Field = field_view_field ('node', $ node, 'Field _ image', $ display );
For the body Field, print the trim value:
$ Display = array (
'Label' => 'ddn ',
'Type' => 'text _ summary_or_trimmed ',
'Settings' => array ('trim _ length' => 150 ),
);
$ Output = field_view_field ('node', $ node, 'body', $ display );
Print render ($ output );
Additional supplement:
If you only want to obtain the field value, you can use field_get_items and use field_view_value to print the specified $ delta value. For example, the first one. The sample code is as follows:
$ Node = node_load ($ nid );
$ Field = field_get_items ('node', $ node, 'Field _ name ');
$ Output = field_view_value ('node', $ node, 'Field _ name', $ field [$ delta]);