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.
<blockquote><?php
$MrNode = node_load(array("nid" => 415));
print $MrNode->body;?>
</blockquote>
<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:
"The very existence of flame-throwers proves that some time, somewhere, someone said to themselves, 'You know, I want to set those people over there on fire, but I'm just not close enough to get the job done.' " - George Carlin-- The Invention of Flame Throwers
Here is a link to the node which we are using. Cool, huh?
Comments
Different use of node_load
Can it include dynamic pages/nodes?
Could you apply this code to a page that was dynamic? Lets say you had a node that was printing the 5 most recent items in a certain taxonomy of your choice through a php command. Would there be any problems with the double layer of PHP or would it work fine? It might be a nice way to create overview pages with recent news and attach that to posts where it was relevant. Say on your blog, any post on Drupal you could quickly include to print the node that has your PHP code to display the titles of the most recent posts in the drupal section of your taxonomy.
Or say a sports blog/news site could post about sports teams (say the Braves) and include underneath the most recent "Braves" stories.
Not that I am that big a sports fan, but thats just an example.
thanks!
Good question.
Post new comment