Drupal 7 override page.tpl.php for specific content type

Updated: 11 November, 2021 seen 3,833

Working on project I needed a function to override default page.tpl.php for specific content type. I did use theme_hook function some 2 years ago, and I knew it's achievable in Drupal.

Doing quick Google search found a Drupal tutorial https://drupal.org/node/249726 -  don't know is it problem with my project, but that didn't work as expected, and after brief investigation I found a solution: https://drupal.org/node/1089656#comment-4662688 

 

Create a new page.tpl.php for your specific content - for example page--acommodations.tpl.php

In your template.php file add:

function YOURTHEME_preprocess_page(&$vars, $hook) {
if (isset($vars['node'])) {
// If the node type is "accommodation" the template suggestion will be "page--accommodation.tpl.php".
$vars['theme_hook_suggestions'][] = 'page__'. $vars['node']->type;
}
}
 
Remember to change YOURTHEME to name of your theme. Clear all caches, and now you can start theming page for specific content type.