What code do I need to use in order to print a specific nodes content in a block i.e. node 16 - I've hunted & can't find a how to anywhere!

Comments

nevets’s picture

Given that you know the node id (nid), the following code will load the contents of a node.

<?php
$nid = 16;
$node = node_load(array('nid' => $nid));
// At this point you can directly access any of the nodes member.
// For example if you want to print just the teaser
//  print $node->teaser;
// To print just the body
//  print $node->body;
?>

If you want to print the complete formatted node then add this code after the call to node_load()

<?php
print node_view($node);
?>

All thogther this would look like

$nid = 16;
$node = node_load(array('nid' => $nid));
print node_view($node);

ideogram_nl’s picture

It's ofcourse also possible to use views to print node-content in a block. No coding involved and most likely, no extra modules.

seaneffel’s picture

If you want to use Views then you're talking about downloading CTools and Views and enabling four modules or so, then creating a view and noodling with the filters and finally noodling with the block visibility settings. Sure, it gives you finer control of the output but that's a lot of overhead if all you need is a quick thing.

A couple of the snippets on this page are pretty useful as is and don't require modules at all.

ideogram_nl’s picture

That's why I wrote: "most likely". There might be other readers of these posts who have "views" also already enabled. It would have been a shame not to mention this possibility for those readers/

The filtering is not complicated, so for those interested:

Contextual filters » Content: Nid » When the filter value is NOT available » provide default value » Content-id From URL

tclineks’s picture

Something Like this, in a custom PHP block.

$_node = node_load(array('nid' => 16));
echo $_node->body;
seaneffel’s picture

This also works perfectly in Drupal 6, btw.

tomski777’s picture

works perfectly - I knew it would be simple in the end!

><>tomskii
><>www.mutinyarts.co.uk

djensen47’s picture

What if you wanted to start with a path alias instead of a node id?

nevets’s picture

The following code coverts an alias to a 'normalized' path.
Since it is looking for nodes, it checks to see it the return path referes to a node.

<?php
$path = drupal_get_normal_path($alias);
		
if ( strpos($path, 'node/') != 0 )
{
  // Not a node, decide how to handle it.
}
else {
  // It's a node, get the node id (nid)
  $nid = substr($path, 5);
  // At this point you can use node_load like this
  $node = node_load(array('nid' => $nid));
}	
?>
ecksley’s picture

I know this is an old node, but I found it when researching the topic 6 years later so others may as well.

For Drupal 7 the following will output the full node

<?php
$nid = 16;
$node = node_load($nid); 
$nodeView = node_view($node, 'full'); 
print drupal_render($nodeView);
?>

However you can output individual fields like the body by drilling into the array as follows:

<?php
$nid = 16;
$node = node_load($nid); 
$nodeView = node_view($node, 'full'); 
print drupal_render($nodeView['body']);
?>
AgaPe’s picture

thanks! :)

Thien Chern’s picture

Thank you so much..

ptkirby’s picture

I have a question regarding accessing data within the $node object above.

Until I found this post, I was attempting to display node contents by accessing the information through $node->body. However, no matter what way I attempted to print the results, nothing would show up.
I tried drupal_render($node->body) and drupal_render(node_view($node->body));

Your method above worked, but that leads me to my question:

What exactly was I trying to access by using $node->body? I understand that node_view() outputs an array, and by using $nodeView['body'] you were able to access a position in said array. However, why didn't my method work?

Thanks

morgandival’s picture

This is very handy for reusing snippet code if you have defined 'snippet' as a content type.

keti-1931’s picture

i have a page from custom module but its having path and not nid so how to go about it.
thanks,
ketki

nevets’s picture

You custom module would need to also provide a block.

mrtoner’s picture

John, that's just what I needed.

bosspetta’s picture

I have a solution for filter content by number of characters.

<?php $_node = node_load(array('nid' => 87));
print substr(strip_tags($_node->body),0,300).'...'; 
?>

I hope it will be useful.

Regards.

nevets’s picture

Note this solution also removes any html markup.

seaneffel’s picture

Is there any tweak that would preserve markup?

occupant’s picture

Try the CCK Blocks module:
http://drupal.org/project/cck_blocks

gurpreet2000’s picture

thanks you