I tried to change Different page templates depending on node type in Drupal 6.X.
So I used below code which is to change Different page templates depending on node type in Drupal 5.X.
But it didn't work for Drupal 6.X. If anyone can help this problem, please let me know.

Thank you,

function _phptemplate_variables($hook, $vars) {
  switch ($hook) {
    case 'page':      
      // Add page template suggestions based on node type.
      // page-nodetype-news.tpl.php
      if (arg(0) == 'node' && is_numeric(arg(1))) {          
        $result = db_query('SELECT type  FROM {node} WHERE nid = %d',arg(1));
        $record = db_fetch_object($result);
        $node_type = $record->type;        
        $suggestions[] = 'page-nodetype-'. $node_type;
        $vars['template_files'] = $suggestions;        
      }
    break;
  }
  return $vars;  
}

Comments

dvessel’s picture

Read the docs. And the node object will be available within the "page" preprocessor through $variables['node'] when viewing a node. db_query is no good.

joon park
www.dvessel.com

lgllee’s picture

Thank you for your quick answer.

I read the docs but I don't know what should I do.
Also I don't know exactly what you say.
If you can, please give me more advices.

Thank you,

Mercury500’s picture

This is a dupe of http://drupal.org/node/155557.

I'm looking for the same thing. And also have searched the Drupal archives. Your link to the docs was unhelpful, even though I know a bit about PHP.

Can you point me (us) to the code needed to implement this seemingly easy task in Version 6.x?

Different page templates (page.tpl.php --> page-oldstory.tpl.php) depending on the different node types.

Thanks.

dvessel’s picture

The docs should make sense if your familiar with php. It explains in what type of function it must be set in (preprocess functions) and the purpose of template suggestions.

But here it is..

function phptemplate_preprocess_page(&$variables) {
  if (isset($variables['node'])) {
    // When viewing a node focused as a full "page", it will suggest the node type.
    // For example, a node type of "some_foo" will search for "page-some-foo.tpl.php".
    $variables['template_files'][] =  'page-'. str_replace('_', '-', $variables['node']->type);
  }
}

Note that it mentions that _phptemplate_variables was deprecated in favor of these new preprocess functions. It's linked from the page about template suggestions.

joon park
www.dvessel.com

Mercury500’s picture

Thank you very much dvessel. It works perfectly.

I spoke too soon, but not really. This is the issue:

Adding this to my template.php file (at the bottom) caused the edit functions to break. Edit pages of nodes would come up, but submit buttons save and delete would produce blank pages.

In troubleshooting, I turned off just about every non-core module and disabled all caching and cleared the caches. Nothing worked. Then I tried removing this new section from my template.php. And that worked.

But here's the thing - my template.php from Ability does not have a closing PHP (?>) tag. So, placing this code, as is, resulted in a PHP error. So, I had to close the template.php original code and then placed this as posted. That worked. Except for the edit problems I discovered.

I then tried removing the extra closing (?>) tag and removing your opening PHP tag - and now it works, including the edit functions. (So, I now have only one opening PHP tag and one closing PHP tag)

I just wanted to note this for other users in case it doesn't work for them right away depending on their theme files. I have no idea if this is "problem" is function of the new code or the Ability theme.

m

dvessel’s picture

Anything posted on here has the opening and closing tags so it highlights the markup. What you just discovered is documented. Only the opening tags are needed.

joon park
www.dvessel.com