0

[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.

apaderno
  • 28,547
  • 16
  • 75
  • 90
technoTarek
  • 3,218
  • 2
  • 21
  • 25

3 Answers3

3

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
jakraska
  • 751
  • 3
  • 6
  • Thank you. That works. Any hints on how I could target just the content of the node (as opposed to the entire page) a la node.tpl.php? Should I use phptemplate_preprocess_node? I want it to apply my main template (page-node-my_content_type.tpl.php) to all of that content type including the page in question. However, I want Drupal to look to the suggestion of another file for specific rendering of the content section of a single node (5555). What you gave is for customizing the entire template for the node as opposed to just the content section. – technoTarek Jan 22 '12 at 21:53
  • Disregard the follow up questions. Using the phptemplate_preprocess_node function did what I needed. Thanks again. – technoTarek Jan 22 '12 at 22:09
0

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..

uzair
  • 766
  • 1
  • 13
  • 25
0

    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);
  }
}