A Practical Tutorial on Working with Nodes

Web Development:

Every Drupal user, at one time or another, has fantasized about having full control over their nodes (That was an odd sounding sentence wasn't it?). Well, fantasize no more, because at this early hour in the morning I will show you one damn useful trick.


Let's say I wanted to include the body of a single node into multiple pages, and have the option of nesting those nodes into infinity. Such an include would not only have the advantage of displaying with perfect consistancy across multiple pages; most importantly, it would only need to be updated at one location. And let us also pretend that piece of content happened to be a quote by George Carlin.

Though the Carlin's quote has a path alias, its fairly easy to figure out that its node ID is 415. How do you find a node ID? Well its simple, silly. Usually you can just look at the URL of the node. The number is the node ID. (e.g. yoursite.com/node/666 = node id 666).

Armed with our node ID of 415, I am now able to load the noad.

$MrNode = node_load(array("nid" => 415));
print $MrNode->body;?>
<em>-<?php print $MrNode->title;?></em>

In line two, we load our node... in this case 415. $MrNode, the name for the node we loaded is actually an array of information. To print him properly, we must be more specific than simply "print $MrNode;". So in this case, we've printed $MrNode's body (i.e. $MrNode->body ). Finally, on line 4, we print the node's title within italics. Below, you see this beautiful code in action:

415)); print $MrNode->body; ?>
-- title;?>

Here is a link to the node which we are using. Cool, huh?