Custom page.tpl.php Template

How do you create a custom page template based on content type for a Drupal 7 site? Simply start from the page.tpl.php, the Drupal default page template. The process of creating a custom page template involves:

  1. Renaming the template file
  2. Creating a pre-processor function
  3. Clearing cache

Renaming the template file

In this step, you add the machine name of the content type to the file name i.e. page.tpl.php:

page–content-type-machine-name–.tpl.php

Create Pre-processor Function

This function is placed in template.php as follows:

[php]
function themename_preprocess_page(&$vars, $hook) {
if (isset($vars[‘node’]->type) && !empty($vars[‘node’]->type) && ($vars[‘node’]->type == ‘content_type_machine_name’)) {

$vars[‘theme_hook_suggestions’][] = ‘page__’ . $vars[‘node’]->type;
}
}
[/php]

Replace the ‘themename‘ and ‘content_type_machine_name ‘with your own values i.e. the name of your theme and the content type respectively.

Clear Cache

Clear cache under admin/config/development/performance to allow for the reloading of template file.

April 23, 2014

Posted In: Information Technology

Tags: ,

Leave a Comment