User login

Different page templates for different content types

Once upon a time there was a drupal themer working on a dark and stormy night, he sat there wondering how to make different page templates for certain content types on his site. He thought about how the drupal template system seems to be missing this for some reason... Suddenly, he had a vision! Like a ray of light bringing, hope and happiness down from the heavens!! oh yes!!

Resolution

<?php
// Add additional template suggestions
function _phptemplate_variables($hook, $vars) {
  switch ($hook) {
    case 'page':  
      // Add page template suggestions based on node type, if we aren't editing the node.
      if ($vars['node'] && arg(2) != 'edit') {
        $vars['template_files'][] = 'page-'. $vars['node']->type;
      }
      break;
  }
  return $vars;
}
?>

This means if you have a content type called event and you want it to have a custom page layout, just copy page.tpl.php to page-event.tpl.php.

got it?

just dump the code into template.php, then make a page template called page-[CONTENT TYPE].tpl.php

and drupal will use that instead

Searched words: 
Different page templates for different content types page.tpl for content type drupal 5 5.x theming

Comments

Can this be achieved for node.tpl

Hi,

Can we have different node templates based on the node types. Is this possible in Drupal?

Yes. That is built in.

As of Drupal 5, just copy your node.tpl.php file to whichever node-CONTENT_TYPE.tpl.php and that template file will be used for theming nodes of that type.

For example, node-event.tpl.php.

What about drupal 6.x

I could not make this work for version 6.4. Should it work?

It works. I used a Zen

It works.
I used a Zen subtheme and had to use the right function name to override.
Thank you.

Thank you so much for reporting back!

That means a lot, even though we weren't able to test 6 for you ourselves. Glad it works!

I cant

It doesnt work for me hope you help me

Didnt work

I get an error:

Fatal error: Cannot redeclare _phptemplate_variables() (previously declared in /home/content/xxx/themes/celju/template.php:21) in /home/content/xxx/themes/celju/template.php on line 81

I am using 6.3.

Any ideas? Thanks.

duplicate function name

Hi Matt, is the theme you are using celju? If so just find where in template.php _phptemplate_variables() is defined and add the code there, though this is a post for Drupal 5.

Thanks

Yes, celju and yeah I had to take out the other code.

This works better...

I couldn't get your snippet to work in D6 with a Zen subtheme.

This method worked though.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.