I am struggling to work out how to print a node body on a page.
I normally use something like this print $node->field_manufacturer[0]['view'];
but I am unsure how to print the body of a node.
How can I achieve it?
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityYou can view the whole structure and content of a node by writing:
<pre><?php print_r($node); ?></pre>
And then choosing what deems appropriate.
To print the node body, you should use $node->content['body']['#value']
.
For Drupal 7, a better style is to use field_get_items. E.g.:
<?php
$body = field_get_items('node',$node, 'body');
print $body[0]['value'];
?>
Just try this for Drupal 7
<?php print render($content['body'])?>
render
would be typically used inside .tpl.php
templates to format the elements using layout information in the render array, see e.g. themery.com/dgd7/advanced-theming/render
If $node
is a node object obtained for example with node_load()
, in Drupal 7 this is the structure of $node->body
.
The language set for the node I used in the screenshot is English, and the input format is full HTML. As far as I can see, the content of the body is always contained in the "und" index, whatever language has been set for the node.
In Drupal 6, $node->body
is a string.
If you implemented hook_nodeapi('view')
(Drupal 6) or hook_node_view()
(Drupal 7) the content of the body is found, respectively, with $node->content['body']['#value']
, and $node->content['body'][0]['#markup']
.
drupal 7 :
<?php print $node->body['und'][0]['value'] ?>
The following usually works:
<?php print $node->body['value']; ?>
Perhaps a little late, but try the following, if you are using Drupal 6.
<?php print $node->content['body']['#value'] ?>