Changing page.tpl based on node type

Today I needed to add a new candidate page.tpl file based on node-type. I found this code snippet and modified it slightly.

function phptemplate_preprocess(&$vars, $hook) {
	switch ($hook){
		case 'page':
		  // Add a content-type page template in second to last.	
			if ('node' == arg(0)) {
				$node_template = array_pop($vars['template_files']);
				$vars['template_files'][]  = 'page-' . $vars['node']->type;
				$vars['template_files'][]  = $node_template;
			}
			break;
	}
	return $vars;
}

My tweak puts the new template in the middle of the candidate templates array so that I can still have specific page.tpl files for specific nodes. The devel module tells me my candidate files are now:

page-node-4.tpl.php < page-story.tpl.php < page-node.tpl.php < page.tpl.php

This works on Drupal 6.

This is a very cool tip,

This is a very cool tip, brother

Drupal 6

I don't see any function named like this in template.php
I found a phptemplate_preprocess_page, though.

Should i replace the one I found or add the one you provide beside it ?

Thanks.

Nope. Just create this

Nope. Just create this function and Drupal will find it.

Thank you

This helped me greatly today. Much appreciated.

Its important to remind

Its important to remind people that they need to clear the drupal cache before they will see any changes.
admin/settings/performance

Thanks and an echo

Thanks, this worked wonderfully in Drupal 6 after I cleared my cached files under admin/settings/performance

Helpful

Thanks for posting this... it was very helpful! Especially the reminder to clear your cache.

Amazing formula to render the layout drupal content-type wise

Hye Floydm,

This is the great formula that I appreciated to look for the drupal.

I've updated the drupal 6.4 to 6.10 which is the latest release of the drupal nowadays.In 6.10, the development team has fixed some issues which had been presented in the prior release of the drupal.

I read through the internet to render the layout node's content type wise. It says that, "node-[content-type].tpl.php" will render the layout, node's content-type wise. But it won't work. I also tried to use page.[content-type].tpl.php without implementing your function in template.php file.

Many thanks,

Web-Farmer
http://www.letsnurture.com

Thank you

awesome!

The Looking Glass Wars

If I had to choose my favorite book it would definitely be "The Looking Glass Wars." http://www.ebook-search-queen.com/ebook/The+/The+Looking+Glass+Wars.all.html
"The Looking Glass Wars" is an amazing adventure of love, loss, forgiveness, hate, abilities, authority, but most of all trust and disobedience.
Alyss Heart is the main character, she is Queen of Wonderland. Wonderland is the same place as Alice's Wonderland. Alyss Heart is the same woman as Alice, except Alyss has black hair, lives in Wonderland, and she is Queen.
This book is definite must-read, as I was reading it I just couldn't put it down. So the next time you go to a book store, look for "The Looking Glass Wars" by Frank Beddor.

Thanks!

Awesome... saved the day!

Thanks,
BC

awesome!

very interesting

Didn't you think of creating a journal based on your blog (many people already do so - have a look at their experience at the search engine on pdf periodical)? In comparison with the blog, journal is more convenient to read :)

Brilliant! Exactly what I

Brilliant! Exactly what I needed, many thanks.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <b> <blockquote>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.