Overriding node.tpl.php for one single node

deane - January 28, 2007 - 05:22

I want a specific "node.tpl.php" file to be used for a specific node. This particular node has to do all sorts of stuff, for which I need to write some significant PHP.

I've been doing some research, and I found that I can name a file "page-node-9.tpl.php," however, this overrides "page.tpl.php" which is not what I want. This overrides the entire page structure, where I just want to override the interior content section that's normally populated by "node.tpl.php".

I tried naming a file "node-node-9.tpl.php" but that didn't work.

(My understanding is that "node.tpl.php" effectively embeds itself into "page.tpl.php," correct? This being the case, "node.tpl.php" is the file I want to override for one single node.)

I could just do something like this in the "node.tpl.php" file --

if($node->nid ==0) { ... }

-- but that seems hack-ish.

Any ideas?

...

chelah - January 28, 2007 - 11:05

If the content type is 'abc' then the node template should be 'node-abc.tpl.php'.

No, a single node...

deane - January 28, 2007 - 13:19

...not a content type. This node is a page, but I don't want override all pages, just node #9.

...

chelah - January 28, 2007 - 15:58

You mean a node of content type 'page'? Then you may copy 'node.tpl.php' to 'node-page.tpl.php' and hack the later.

Or you can use contemplate module.

In this case some hack is required. If you don't want to hard code '9' then use drupal function variable_get(). You have to call variable_set() first somewhere else.

if ($node->nid == variable_get('my_special_node', 0))
{
  // the special node theme
}
else
{
  // default theme
}

What's wrong with an if statement?

jwolf - January 28, 2007 - 15:44

Why not use an if statement as you had mentioned?

Maybe you can do an if statement which will call an include (.inc) file containing the changes that you want to make for the single node; this way you won't have to put all the code for that one node into node.tpl.php, just include it when needed.

It's just a thought.

 
 

Drupal is a registered trademark of Dries Buytaert.