In Drupal, we can determine the template files of the page which is going to be rendered. Add the following code in your template.php.
... function <theme>_preprocess_page(&$vars) { // Drupal 7 print_r($vars['theme_hook_suggestions']); exit(); // Drupal 6 print_r($vars['template_files']); exit(); } ...
Refresh the page in browser, you should get sth like
Array ( [0] => page-node [1] => page-node-184 )
The means Drupal will apply page-node-184.tpl.php for the rendering page, if it is not found, page-node.tpl.php will be used. So if you want to add a new template, just append new element to the $vars[‘template_files’].
The following example will add the page-<content-type>.tpl.php to the vars[‘template_files’].
... function <theme>_preprocess_page(&$vars) { // Add page template for different content types if (isset($vars['node'])) { // Drupal 7 $vars['theme_hook_suggestions'][] = 'page__'. $vars['node']->type; // Drupal 6 $vars['template_files'][] = 'page-'. str_replace('_', '-', $vars['node']->type); } } ...
If you have 2 content types which are named as store and convenient_store, the corresponding page templates are
Drupal 7
- page--store.tpl.php
- page--convenient-store.tpl.php
Drupal 6
- page-store.tpl.php
- page-convenient-store.tpl.php
Done =)
Reference: Creating Custom Content Type Page Templates with Drupal & PHP
Update @ 2013-02-25: Add Drupal 7 implementation as stated in this comment.
This doesn’t work for me (Drupal 7). This line outputs nothing:
print_r($vars[‘template_files’]);
LikeLike
Hi Tronno,
The article only works for D6. For Drupal 7, use the following instead.
Reference: Drupal 7 Template Suggestions
Kit
LikeLike
how to applay a php file in a php file
LikeLike
not sure about what you mean.
LikeLike
And remember to clear cache to make it work.
LikeLike
Many thanks ykyuen, it worked! =)
LikeLike
you are welcome =)
LikeLike
this code does not work in drupal7 so please update .
LikeLike
Please refer to this comment for D7.
LikeLike