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

Updated: 11 November, 2021 seen 3,909

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.

 

 

about

Hi! My name is Reinis Fischer (38), a proud dad and devoted husband. CEO and Founder of Terramatris crypto hedge fund, drone enthusiast, world traveler, photographer, and passionate lover of Georgian cuisine (vegetarian).

An ex-pat living in Georgia since 2011, I trade stocks, take photographs, work out at the gym, and many more. Here I write about travel, finance, and other things that might interest me.