Sometimes it’s handy to use a different page.tpl.php for a certain content type. Maybe the page has a different layout, needs additional regions not used on other pages, etc…
- For starters, lets use some example names/variables, lets say:
- The theme I’m using is called ‘myTheme’
- The ‘content type’ I have set-up is called ‘News Article’
- The ‘machine name’ for my ‘content type’ is ‘news_article’
Solution
- After you have created your content type (i.e. ‘News Article’)…
- Copy your page.tpl.php page and rename it ‘page-’ + ‘machine name for your content type’ + ‘.tpl.php
- So if the ‘machine name’ for my ‘content type’ was ‘news_article’, my file would be ‘page-news-article.tpl.php’
- Note: the underscore has been changed for a hyphen.
- Open your template.php file and add the following code
function myTheme_preprocess_page(&$vars) { if (isset($vars['node'])) { $vars['template_files'][] = 'page-'. str_replace('_', '-', $vars['node']->type); } }
Note that the function name starts with the name of your theme, so in this case it’s ‘myTheme_preprocess_page’, but you’ll need to change this to the name of the theme you are using.
All you need to do now, is fluch the cache to rebuild the theme structure, and you’re off…
Drupal 7 Version
The Drupal 7 version is slightly different...
function myTheme_preprocess_page(&$variables) { if (isset($variables['node'])) { $variables['theme_hook_suggestions'][] = 'page_'.$variables['node']->type; } }
Again, if my content type as 'News Article' with a machine name of 'news_article', then I would name the tpl.php file ‘page-news-article.tpl.php’... We don't need to convert the underscore to a hyphen in Drupal 7.