Override how a Drupal Page is Displayed Based on Content Type

Have you ever wanted to change the way Drupal displays pages for certain content types? Drupal has built-in functionality that allows you to add additional template files to your Drupal themes. However, as of Drupal 6, it will not allow you to override page.tpl.php based on content type. You change this by adding a small amount of code to your Theme's template.php file:

function THEMENAME_preprocess_page(&$vars, $hook) {
  $template_files = array();
  foreach ($vars['template_files'] as $file) {
    $template_files[] = $file;
    if ($file == 'page-node') {
      $template_files[] = 'page-'. $vars['node']->type;   
    }
  }
  $vars['template_files'] = $template_files;
}

After adding this code, you'll be able to create a separate copy of page.tpl.php and modify it for specific content types.

For example, if you have a content type called "Landing Page" you could make a copy of page.tpl.php called page-landing_page.tpl.php and change it to display Landing Pages differently. It's important to note, the name of the file depends on the machine readable name for the content type. Typically, this name is the same as the human readable name except all characters are lowercase and spaces are replaced by underscores.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CONTACT US   734.393.4696

About

We eat, drink, and live Drupal.

Commercial Progression exists as Alex Fisher and my team of trusted web developers, graphic designers and freelancers.

We are 100% focused on Drupal and want to help you leverage it to turn great ideas into reality. We're based in Farmington Hills, MI and have been in business since 2008.

Learn more about us

Blog

I'll be participating on a panel discussion about the decision to use Wordpress or Drupal for your organization's website...
Sometimes I'm so focused on getting high quality client work out the door it's hard to focus on my own website...
DrupalCamp Michigan Official Sponsor!