Printing a node with node_load()

You can print a node using the node_load() function. You should use it in conjunction with the node_view() function to prepare the correct output. If you don't use node_view() you might end up getting an error like: "Recoverable fatal error: Object of class stdClass could not be converted to string in include() (line 10 of ..."

$view_mode = 'teaser'; // Or 'full' for example
$node = node_load(123);
$view = node_view($node, $view_mode);
print render($view);

 

In the below example I wanted to show a short author biography below an article. To do this I used the Entity module to associate the article with an author profile. I didn't want to use the default teaser display, so using the Display Suite module I created a custom View Mode which had machine name of author_bio.

In the article content template (node--article.tpl.php) I included the below:

$view_mode = 'author_bio';
$nid = $node->field_author['und'][0]['entity']->nid;
$node = node_load($nid);
$view = node_view($node, $view_mode);
print render($view);
Justin Chevallier

Justin Chevallier

Avid Drupal site builder & user for +10 years.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.