Drupal 6 page template suggestions

In this Drupal tutorial we will look at how to specify a page template file (page.tpl.php) based on the content type of the particular node in question.

This technique will allow us to have different page layouts for different content types (like different templates for regular pages as opposed to stories).

The technique is called template suggestion and will indicate to Drupal which files it should look for when determining what page template to use for a node based on the node's content type.

Insert the following code into a/the template.php file located in the directory which contains your theme:

<?php
 
function phptemplate_preprocess_page(&$vars)
{
  if (isset($vars['node']))
  {
    $vars['template_files'][] = 'page-'. str_replace('_', '-', $vars['node']->type);
  }
}
 
?>

That code will suggest to Drupal to look for files of the form page-content-type.tpl.php when theming the output to be sent to the browser.

For example, if we had a content type called "story", Drupal would look for page-story.tpl.php before defaulting to page.tpl.php

Remember to clear your cache for the changes to take effect.

We hope this tutorial was useful.

Thank Tutorial Arena for This Tutorial.
Show your appreciation with a +1...