Drupal 6 Custom content type page theme

If you ever wondered how to change the page.tpl.php for a custom content type you've found out that by default Drupal 6 doesn't know how to do it.

It's "fixed" easily by adding the following code:

  1. /**
  2.  * Override or insert PHPTemplate variables into the templates.
  3.  */
  4. function phptemplate_preprocess_page(&$vars) {
  5.   // Add per content type pages
  6.   if (isset($vars['node'])) {
  7.     // Add template naming suggestion. It should alway use hyphens.
  8.     // If node type is "custom_news", it will pickup "page-custom-news.tpl.php".
  9.     $vars['template_files'][] = 'page-'. str_replace('_', '-', $vars['node']->type);
  10.   }
  11. }

Thanks dvessel

Comments

Thank you!!

Thanks, I found this very helpful.
Entering the original function as is breaks many portions of the admin section, but if you rename the "phptemplate" portion of the function name to "mysite" where it is the name of your site it works perfectly.

The recommended way is to replace "phptemplate" with the name of your theme.

hhm... its not working for me. i have a content type 'affiliate' and wanted to call a template 'page-node-affiliate.tpl.php' base on node type('affiliate').

i only received a blank page...

please help...

thanks,

vsotto

You've got the wrong naming convention. You can create a template for a page and/or a node. If you want a page, then it's page-[name].tpl.php and if you want to only change the node layout then you need to use node-[name].tpl.php

This worked greart for me. I've been looking for hours!

Thank you thank you thank you! What an elegant and slim solution, just what I needed!

many thanks

Don't forget to Clear Cache!

Thank you so much, I have been trying to solve this problem for hours now!

Cheers!

Worked like a charm... Thanks!

That is a deadly "Empty Cache". Waste sometime before I found @cool_qosys comment.

Bloody marvelous!
Thanks!

Third class

Awesome great post !

Thank you! Worked smoothly!

thanks worked perfectly

HELP!. It works but replaces the entire page template with the new template. I just want to replace the content portion of it, thus keeping header, footer, etc.

Any ideas?
thanks

node-[name].tpl.php

Sorry I was not completeley specific.
node-[name].tpl.php works for content types but not for taxonomy terms which is my problem. and all the solutions I have tried replace the entire page code not just the content portion of it.

thanks a lot.

Ah, unfortunately you can't do that easily.
One solution would be to split your existing page.tpl in header/footer parts and include them.

Got It!
Thanks a lot for your answer, however I just figured out the answer to my problem, in case anybody is looking for the same issue here's what I did:

function mysite_preprocess_page(&$vars, $hook){
if($hook == 'page' && arg(0) == 'taxonomy' && arg(1) == 'term'){
$vars['content'] = theme('custom_node_taxonomy', $vars['content']);
}
}

and then implement the selected theme into the mysite_theme()

Now I can use my custom tpl to edit term pages. Maybe not the most efficient solution but works fine

cheers.

Thanks a lot for the update ;). Nice snippet.

Pages

Add new comment