// you’re reading...

Tips N Tricks

Drupal 6: Different Page Templates for different Content Types

Given the flexibility of Drupal, a different page template depending on the Content Type is not very demanding. The good news is, it is very much possible. The bad news is you may have to struggle like me to find the simplest, neatest and working method to achieve it.

Reading the documentation and Googling would result in many methods – some are confusing, some are not so neat and others apply to earlier versions of Drupal. Following is my favourite method to setup different page templates, depending on the Content Type, in Drupal 6.

What is a Page Template
This is the template which contains a Node. This must not be mistaken with the node template, for example, a Story node which displays the title, teaser, images, comment form and et al. In Drupal, the default page template is written in the file named page.tpl.php (and the node template is in node.tpl.php).

It is the page template with which you can display three columns for certain content types and two columns for others. It is the page template using which you can dictate the column which will show a Story, a Blog entry, a Forum topic or a Webform. This template is where you insert the Regions for Blocks and Views.

A Neat Solution
Append or write the following code in a file named template.php in your theme’s directory:

<?php
function <theme-name>_preprocess_page(& $vars) {
	if (isset ($vars['node']) && $vars['node']->type == "blog") {
		$vars['template_files'] = array();
		$vars['template_files'][] = 'page-blog';
	}

	if (isset ($vars['node']) && $vars['node']->type == "forum") {
		$vars['template_files'] = array();
		$vars['template_files'][] = 'page-forum';
	}

	if (isset ($vars['node']) && $vars['node']->type == "webform") {
		$vars['template_files'] = array();
		$vars['template_files'][] = 'page-webform';
	}
}
?>

In the above code replace <theme-name>, in the name of the function, with the name of your Theme’s directory. For example, suppose the name of your Theme’s directory is “blue”, then:

function blue_preprocess_page(& $vars)

Now all you need to do is what you are most creative at. Write the templates for each content type. Drop files in your theme’s directory named page-blog.tpl.php, page-forum.tpl.php and page-webform.tpl.php for the Blog entry, Form topic and Webform  respectively. Feel free to set different page layouts and Regions in these files. Note that any Content Type which is not specified in the above code will use page.tpl.php – the default page template. For example, in this case, the Story content type will use page.tpl.php.

Important: Log into Drupal as admin and goto Administrator>Site configuration>Performance and click on the button “Clear cached data” (at the bottom).

I prefer this solution because the template selection logic goes separated into a different file, leaving the Page templates friendly for a designer. Plus, template.php is the logical place to add overrides or specific behaviour. Most likely a seasoned Drupal developer will first look into this file for customizations.

Created or added a new content type? All you need is to add another IF block in the above code and drop a new page template. Before concluding that it is not working for you, do remember to clear the cache.

GD Star Rating
loading...
GD Star Rating
loading...
Drupal 6: Different Page Templates for different Content Types, 8.1 out of 10 based on 86 ratings
Share

Email This Post Email This Post Print This Post Print This Post Print This Post Post A Comment Tweet your comments/question to me @shekharg

Discussion

34 comments for “Drupal 6: Different Page Templates for different Content Types”

  1. thank you very much, it doesn’t get any more simpler than your explication.

    Posted by Alex | April 3, 2010, 9:45 pm
  2. This is good code except it affects the editing form. In my case I do not want to print the $content variable as this is a custom template.

    If you remove the $content variable from your template, it causes the EDIT form of the node to break of the node type you specify. Here is modified code that will not apply to the edit form.

    function drupalmags_preprocess_page(& $vars) {
    if (isset ($vars[‘node’]) && arg(2) != ‘edit’ && $vars[‘node’]->type == “affiliate”) {
    $vars[‘template_files’] = array();
    $vars[‘template_files’][] = ‘page-affiliate’;
    }

    }

    Posted by gripmedia | April 16, 2010, 6:07 am
  3. Worked for me.

    Cheers!

    Posted by markdamo | May 11, 2010, 2:38 pm
  4. Thanks for confirming! Glad that it worked for you too 🙂

    Posted by Shekhar | May 11, 2010, 3:29 pm
  5. I’m fairly new at Drupal, so I may be missing something here, but if I want to use a different page template for my blog content, I just create the page-blog.tpl.php file. Once I clear the caches, the theme uses it without touching the template.php file. I checked my template.php file to see if the functions were created by the theme designer, but I don’t see them.

    I’ve used this on several sites for story and blog content types with no issues. Could these preprocess functions already be in the core code for the standard content types?

    Thanks,
    Alan

    Posted by Alan | June 9, 2010, 7:12 pm
  6. Hi Alan,

    What works is node-blog.tpl.php (for node types). For page templates you will have to use the preprocess_page function.

    For this reason, I made sure that I explain the difference between the node and page templates – please refer to “What is a Page Template” in the above blog post.

    Posted by Shekhar | June 9, 2010, 7:43 pm
  7. How is this fantastic !!!!

    Posted by DIA | August 17, 2010, 9:45 pm
  8. In a situation, each content type has different block regions, some regions are sharing among all content types but some aren’t, then how will look like in block administration page ? All regions will be shown.

    Posted by Adrian | September 15, 2010, 8:15 am
  9. Hi Adrain,
    Sorry for the delay in responding. Let me understand this. The Blocks administration page shows the regions in two ways:

    1. In place, with yellow background
    2. In dropdowns, to assign the blocks.

    Only those regions which are present in page.tpl.php will show in place, with the yellow background.

    But all regions will show in the dropdowns for each Block.

    So if you are saying all blocks will show in the dropdowns, then you are quite right 🙂

    Posted by Shekhar | September 17, 2010, 7:57 pm
  10. Thank you so much…
    It was exactly what I was looking for 😉

    Posted by Davide | November 18, 2010, 1:47 pm
  11. I have done it what you have posted but how do you see whether it is working or not? I am rather new to drupal.

    Thanks.

    Posted by Jonathan | November 26, 2010, 9:39 am
  12. Hi Jonathan, sorry for the delay in responding.

    I have done it what you have posted but how do you see whether it is working or not? I am rather new to drupal.

    Let us take the example of blog (or blog entry). As I explained in the above blog post, create a file named page-blog.tpl.php with the following content in it:

    <html>
    <head>
      <title>< ?php print $head_title; ?></title>
      <?php print $head; ?>
      <?php print $styles; ?>
    </head>
    <body>
      <h1>THIS IS BLOG TEMPLATE</h1>
       <?php print $content ?>
        <?php print $closure; ?>
    </body>
    </html>
    

    Place this file in the directory of your current Drupal theme ( /themes/)

    Next, login into Drupal (say as admin user) and click on Create content. Then click on Blog entry. Fill in the fields and click on Save.

    Subsequently, you should be greeted with a page which starts with THIS IS BLOG TEMPLATE, followed by the content that you typed in (via Create content). If you see a page like this, it’s working!

    Posted by Shekhar | December 1, 2010, 9:59 pm
  13. Thanks for the response! But I have tested it but I still do not see anything.

    Posted by Jonathan | December 2, 2010, 7:35 am
  14. Hi Jonathan,

    Thanks for the response! But I have tested it but I still do not see anything.

    How can I help you here? Give me some pointers – some error messages etc. Hope, after making the changes you did clear the cache as: Goto Administrator>Site configuration>Performance and click on the button “Clear cached data” (at the bottom)

    Posted by Shekhar | December 11, 2010, 2:07 pm
  15. well played. boom!

    Posted by joe | December 18, 2010, 12:42 am
  16. Thanks for this post Shekhar. Awesome stuff!

    Posted by Susheel Chandradhas | January 16, 2011, 10:22 pm
  17. Thanks Susheel Chandradhas. Went through your interesting blog

    Posted by Shekhar | January 17, 2011, 7:29 pm
  18. Thanks! unfortunately not updated very regularly. 🙂

    Posted by Susheel Chandradhas | January 17, 2011, 9:35 pm
  19. Thanks! unfortunately not updated very regularly.

    Same here. The post following the latest post is as old as June 30, 2010 🙁

    Posted by Shekhar | January 17, 2011, 9:39 pm
  20. Thanks for the post and for the information

    Posted by new print head | February 4, 2011, 3:34 pm
  21. Thank you for this! It was the only piece of code that actually worked for me after researching for a long time on Google. Do you mind if I re-post this in my blog?

    Posted by Allison Wong | February 8, 2011, 3:07 pm
  22. Thanks! Allison Wong

    Do you mind if I re-post this in my blog?

    As long as you carry a link to my blog post in your re-post, I have no issues. 🙂

    Posted by Shekhar | February 8, 2011, 5:34 pm
  23. Will try looks good

    Posted by Yogesh | May 2, 2011, 2:46 pm
  24. I know this is an old post, but thanks. Your solution worked great. 🙂

    Posted by Tammie | August 17, 2011, 9:07 am
  25. THANK YOU! This was a lifesaver!

    Posted by Mike z | February 29, 2012, 2:54 am
  26. How to render multiple form in a page?

    Posted by Prabir Mistry | March 5, 2012, 8:02 pm
  27. Prabir Mistry,

    How to render multiple form in a page?

    Try Webform http://drupal.org/project/webform Else you may have to use hook_form api function.

    Posted by Shekhar | March 9, 2012, 7:41 pm
  28. Thanks to share this. Its really helpful.

    Posted by jayendra | March 14, 2012, 7:24 pm
  29. Tried other similar examples but yours is the only one that actually worked for me!

    Posted by Dave_O | April 18, 2012, 9:31 pm
  30. this is the best and clearly article on different page templates for different content types

    Posted by Motty | May 8, 2012, 2:17 pm
  31. Is it working on drupal 7? If not then how its possible in Drupal 7?

    Posted by Subrata | April 4, 2014, 1:17 pm
  32. Hi Subrata,

    The following modified code should work in Drupal 7.

    function _process_page(& $vars) {
    if (isset ($vars[‘node’]) && $vars[‘node’]->type == “blog”) {
    $vars[‘theme_hook_suggestions’][] = ‘page__blog’;
    }

    if (isset ($vars[‘node’]) && $vars[‘node’]->type == “forum”) {
    $vars[‘theme_hook_suggestions’][] = ‘page__forum’;
    }

    if (isset ($vars[‘node’]) && $vars[‘node’]->type == “webform”) {
    $vars[‘theme_hook_suggestions’][] = ‘page__webform’;
    }
    }

    NOTE: Also the name of the template files (that you create in the themes directory) will change to: page–blog.tpl.php, page–forum.tpl.php and page–webform.tpl.php. Note the double hyphens.

    Posted by Shekhar | April 7, 2014, 7:39 am
  33. Hi Shekhar,
    Thank you for your reply. I am very new in Drupal, I have created a template.php in sites/all/themes/custom(“custom” is my theme name). Then i have paste this code

    function custom_process_page(& $vars) {
    if (isset ($vars[‘node’]) && $vars[‘node’]->type == “blog”) {
    $vars[‘theme_hook_suggestions’][] = ‘page__blog’;
    }

    if (isset ($vars[‘node’]) && $vars[‘node’]->type == “forum”) {
    $vars[‘theme_hook_suggestions’][] = ‘page__forum’;
    }

    if (isset ($vars[‘node’]) && $vars[‘node’]->type == “webform”) {
    $vars[‘theme_hook_suggestions’][] = ‘page__webform’;
    }
    }
    inside template.php.
    then I have created page–blog.tpl.php, page–forum.tpl.php and page–webform.tpl.php in sites/all/themes/custom.
    Then please let me know how do I access these 3 templates(page–blog.tpl.php, page–forum.tpl.php and page–webform.tpl.php) or how do I link these 3 templates from nav menu?
    I am waiting for your reply…thank you.

    Posted by Subrata | April 7, 2014, 8:41 am
  34. Subrata,

    Then please let me know how do I access these 3 templates(page–blog.tpl.php, page–forum.tpl.php and page–webform.tpl.php) or how do I link these 3 templates from nav menu?

    It will be difficult to explain this here. Please refer to this link https://drupal.org/documentation/modules/blog for Blog

    Posted by Shekhar | April 7, 2014, 9:23 am

Post a comment

Recent Tweets

Follow Me on Twitter