11

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?

7 Answers 7

3

You 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'].

2
  • I have tried that. It brings up all the information. Specifically the custom field i have created. But i am unsure exactly how to print the body (a field that is always there).
    – Sam Warren
    Jan 6, 2012 at 10:19
  • 1
    Id answer my own questions but restrictions stopping me :# this worked for me: print $node->content['body']['#value'];
    – Sam Warren
    Jan 6, 2012 at 10:34
16

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'];
?>
14

Just try this for Drupal 7

<?php print render($content['body'])?>
2
  • Hello and welcome. Code-only answers are not the best ones possible. Could you explain how exactly this snippet is supposed to answer OPs issue?
    – Mołot
    Jun 3, 2014 at 6:07
  • 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
    – Maine
    Apr 12, 2015 at 19:40
10

If $node is a node object obtained for example with node_load(), in Drupal 7 this is the structure of $node->body.

screenshot

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'].

screenshot

3

drupal 7 :

 <?php print $node->body['und'][0]['value'] ?>
1
  • LANGUAGE_NONE instead of und
    – GwenM
    Aug 23, 2017 at 15:15
0

The following usually works:

<?php print $node->body['value']; ?>
1
  • Just tried that. No errors appear but neither does any content at all except for a ">" Any idea what could be causing this?
    – Sam Warren
    Jan 6, 2012 at 10:31
0

Perhaps a little late, but try the following, if you are using Drupal 6.

     <?php print $node->content['body']['#value'] ?>

Not the answer you're looking for? Browse other questions tagged or ask your own question.