2

I'd just like to print out the body of a specific node (no title or other fields). Am I doing this right?

echo node_load(127)->body['und'][0]['value']

I can't seem to find the answer in any of the Drupal docs.

2 Answers 2

5

Looking with the Devel module to the structure of a loaded node, I notice that "body" contains the following values:

screenshot

The first array index is the language ID for the language set for the node; it could be also LANGUAGE_NONE (the string "und") for nodes for which the language is set to "neutral." That array index contains an array with the following indexes:

  • "value": the content of the "body" field
  • "summary": what set as summary (or teaser) for the node
  • "format": the input format associated with the "body" field

In the node I used as example, if you try accessing $node->body['und'][0]['value'], you would access an undefined value. You should use $node->language to know the language set for the node; you code should be similar to the following one:

$node = node_load(127);
$body = $node->body[$node->language][0]['value'];

As for printing the body content, the "body" field contains the text before the input filter is applied; you should use code similar to the following one, to print the "body" field:

$node = node_load(127);
$language = $node->language;
print check_markup($node->body[$language][0]['value'], $node->body[$language][0]['format'], $language);
2
  • Should the second parameter to check_markup be $node->body[$language][0]['format']
    – user5013
    Feb 27, 2014 at 19:36
  • You are right: The second parameter is the input format being applied.
    – apaderno
    Feb 27, 2014 at 19:44
4
$node = node_load(127);
print $node->body;

Please, note, this outputs just the content of the Body textarea as input in the node form.

To output the fully themed node without title and links would require a separate template file, its introduction via a preprocess function, and a call to

print theme('node', $node);
4
  • OK, but $node->body is an array yeah? Can you just print it out like that?
    – Simon East
    Jul 28, 2011 at 6:48
  • Nope, it's text/HTML/PHP (depending on the input filter). It's just the content of the Body field in the node form.
    – Jan Tomka
    Jul 28, 2011 at 6:51
  • In Drupal 7, I'm pretty sure it is an array because print_r on $node->body shows an array structure like what I've used in the original question. I think I tried what you suggested, but it only printed 'Array' to the screen.
    – Simon East
    Jul 30, 2011 at 12:32
  • Sorry, that was for d6. Retagged your question.
    – Jan Tomka
    Jul 30, 2011 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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