Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print node teaser from nid

How can I print a teaser from a specific nid? It's driving me crazy.

I tried this:

$teaser = TRUE;
$page = FALSE;
$nid = 20;
print node_view(node_load(array('nid' => $nid)), $teaser, $page, FALSE); 

but the only output is 'Array'.

I also tried this:

$node = node_load(20);
$teaser_content = $node->body['und']['0']['summary'];
print $teaser_content;

But this is only giving me the summary of the node, not the teaser specified with <!--break-->.

like image 255
jroeleveld Avatar asked Jan 17 '23 10:01

jroeleveld


2 Answers

In Drupal 7 there is no $teaser argument to the node_view() function, instead there is a $view_mode argument which takes a string (usually teaser or full, the default is full). The code you're currently using would work perfectly for Drupal 6 though.

This code will work for Drupal 7:

$view_mode = 'teaser';
$nid = 20;

$node = node_load($nid);

print render(node_view($node, $view_mode)); 
like image 134
Clive Avatar answered Mar 04 '23 10:03

Clive


Use the render() function.

$teaser = TRUE;
$page = FALSE;
$nid = 20;
print render(node_view(node_load(array('nid' => $nid)), $teaser, $page, FALSE)); 

Be careful using node_view() directly on node_load() as it'll whitescreen if node_load() fails to successfully load the node.

like image 26
Ben Swinburne Avatar answered Mar 04 '23 10:03

Ben Swinburne