Q: How can I override my theme's node template file, node.tpl.php, just for one specific node id?
A: In your theme, override phptemplate_preprocess_node() and provide a template suggestion for 'node-[nodeId]', and then flush cache.
It is easy to provide an alternative node.tpl.php template for your different content types by naming your new node template file along the lines of node-[nodeType].tpl.php, placing it in your theme folder and then clearing you cache. Drupal automatically handles all of this for you.
But how can you provide an alternative node template file for just a single node instance, e.g. for node id 22, just adding a template file named node-22.tpl.php doesn't work?
By default drupal does not handle this, but a solution can be found here, in the comment by psynaptic:
http://drupal.org/node/190815
The solution involves overriding THEME_preprocess_node() and manually providing drupal with a 'template suggestion'. Just place the following code into your theme's template.php file remembering to replace 'THEME' with the name of your theme (or 'phptemplate'). template.php will be found in your theme's folder:
/**
* Override or insert variables into the node templates.
*
* @param $vars
* An array of variables to pass to the theme template.
* @param $hook
* The name of the template being rendered ("node" in this case.)
*/
// Replace 'THEME' with your theme's name or 'phptemplate'
function THEME_preprocess_node(&$vars, $hook) {
$node = $vars['node'];
$vars['template_file'] = 'node-'. $node->nid;
}
Remember to flush you cache after making the change!
Now you should be able to create node template files named like node-[nodeId].tpl.php in your theme folder and have them override node display for specific node ids.
Big thanks again to psynaptic for the tip!
[...] Drupal - node.tpl.php by Node Id | Pride Web Design Cork pridedesign.ie/content/drupal-nodetplphp-node-id – view page – cached Pride Web Design Cork are award winning website designers, online marketing and internet services company, we are based in Cork, Ireland. Tweets about this link [...]