Page Template Suggestions per Node Type

Sometimes its useful to have a page.tpl.php template per node type. While you can have a node-[type].tpl.php be default in Drupal 6, the same does not apply to page level templates.

To add these template suggestions to your theme simply add the following code to template.php, replacing themeName with the name of your theme.

Drupal 6

<?php
function themeName_preprocess_page(&$vars, $hook) {
  if (isset(
$vars['node'])) {
  
// If the node type is "blog" the template suggestion will be "page-blog.tpl.php".
  
$vars['template_files'][] = 'page-'. str_replace('_', '-', $vars['node']->type);
  }
}
?>

Drupal 7

Note the slightly different syntax for D7 template suggestions - they use two hyphens instead of one:

<?php
function themeName_preprocess_page(&$vars, $hook) {
  if (isset(
$vars['node'])) {
 
// If the node type is "blog" the template suggestion will be "page--blog.tpl.php".
  
$vars['theme_hook_suggestions'][] = 'page__'. str_replace('_', '--', $vars['node']->type);
  }
}
?>

Submitted by Jeff Burnz on

Comments

Dan Moriarty's picture

thanks

Thanks for this, I was wondering how to do this with my Genesis subtheme. I also wondered if this works for any custom content type I make, or just default Drupal ones like blog, story, and page. I'll try it out and see.

paul shutt's picture

excellent tip

that's a great tip, very useful. Thanks!

bensue20's picture

Thanks!

Thanks for the tip!

cheers's picture

Nice

Thank you for this, the simple things are always the ones i get stuck on with drupal.

Anonymous's picture

a page template by URL

What if I have ONE cck type, but want to change the PAGE and Node theme based on a URL/URI?
This is my usecase. I do mobile apps. I want to display just a Node in a mobile view, but I want to remove all of the jquery, menu, blocks... and I want to format the page specifically for each mobile device I am working with. I would probably just make the Page.tpl.php file empty for some of the views. At the same time, I would want to make the normal web view work as normal for Desktop views. any thoughts there?

Jeff Burnz's picture

...

I use the mobile tools module for Drupal and Domain Access also, then I build specific subthemes for the mobile devices - so I can give low end mobile devices a low bandwidth experience, and build a more sophisticated theme for more advanced devices. We built a mobile subtheme for Drupal (uses Adaptivetheme as the base theme) just for this purpose:

http://drupal.org/project/adaptivetheme_mobile
http://drupal.org/project/domain
http://drupal.org/project/mobile_tools

Theres another good theme (if you have already built your theme and are not using Adaptivetheme) here http://drupal.org/project/mobile, its pretty basic which is probably what you want for a mobile theme.

You can do what you want with the above snippet and using a node-type.tpl.php for the node, but frankly I much prefer to just build separate themes for the normal Drupal site and the mobile site.