[For Drupal 6] Let's say I've created a content type called "my_content_type". I can override the default template for that entire content-type by creating "page-node-my_content_type.tpl.php". But, what would be the best way to then further customize a single node of that content type (e.g., node 5555)?

I tried the following, but none worked:

  • page-node-5555.tpl.php
  • page-node-my_content_theme-5555.tpl.php
  • node-5555.tpl.php

None of these work. They all continue to use my original content-type template.

3

There are 3 answers

2
jakraska On BEST ANSWER

Drupal's page templates work on a suggestion system. Based on the current URL, an array of possible template files is created. It loops through the array (in reverse order) looking for template files that exists. The first one it finds, it will use.

drupal's theme system provides a hook for you to modify the template suggestions.. open up your template.php and find

function phptemplate_preprocess_page(&$vars) {

the $vars variable is what contains the suggestions, specifically $vars['template_files']

By default the only page suggestions that are available are

  • page.tpl.php
  • page-node.tpl.php
  • page-node-[node_id].tpl.php

As far as im aware, page-node-[node_type].tpl.php does not work by default, so its likely you have already modified the preprocess_page template to added in this functionality.

However if you want to add more specific templates you could do something like this...

function phptemplate_preprocess_page(&$variables) {
  if ($variables['node']->type != "") {
    $variables['template_files'][] = "page-node-" . $variables['node']->type;
    $variables['template_files'][] = "page-node-" . $variables['node']->type . "-" . $variables['node']->nid;
  }
}

this will allow the following hierarchy of template suggestions

  • page.tpl.php
  • page-node.tpl.php
  • page-node-[node_id].tpl.php
  • page-node-[node_type].tpl.php
  • page-node-[node_type]-[node_id].tpl.php
0
uzair On

In Drupal 7 just copy the page.tpl.php template and rename it as

page--node--[node:id].tpl.php

Clear cache and start tweaking..

0
Harry Slaughter On

    function phptemplate_preprocess_page(&$variables) {
      if ($variables['node']->type != "") {
        $variables['template_files'][] = "page-node-" . $variables['node']->type;
        $variables['template_files'][] = "page-node-" . $variables['node']->type . "-" . $variables['node']->nid;
      }
    }

This code should not work because hook_preprocess_page() does not get passed any node information. hook_preprocess_node() does. So you can easily create a custom node.tpl, but you cannot easily create a custom page.tpl for a specific node. Not that I've been able to figure out anyway :)

Later...

In default Drupal, page-node-NID.tpl.php will work with no special coding. On a site of mine, it wasn't working, however, and I used the following code to make it work:

/**
 * Implementation of hook_preprocess_page().
 */
function MYMODULE_preprocess_page(&$variables) {
  // Allow per-node theming of page.tpl
  if (arg(0) == 'node' && is_numeric(arg(1))) {
    $variables['template_files'][] = "page-node-" . arg(1);
  }
}