Custom page.tpl.php template by content type

You can overwrite your default page.tpl.php file for individual content types by using this approach. This can be useful if you are trying to do something like removing the node title from rendering in the page.tpl,php file for a content type, and then including it within your node.tpl.php overwrite file.

Say you've created a new content type called: My Content Type. This will have the machine name my_content_type.

  1. Make a copy of your default page.tpl.php file in your /sites/all/themes/themename/templates folder.
  2. Rename this file to page--my_content_type.tpl.php - note the use of the underscores which is different to naming a node--my-content-type.tpl.php file!
  3. Add the below code to your themes template.php file, replacing the 'themename' & 'my_content_type' values for your own.
  4. Flush the cache to see your page.tpl.php template overwrite.
function themename_preprocess_page(&$variables, $hook) {   
	//Add custom page.tpl.php based on content type 
	if (isset($variables['node']->type) && !empty($variables['node']->type) && ($variables['node']->type == 'my_content_type')){
	// (underscores used in page__ )
	$variables['theme_hook_suggestions'][] =  'page__' .  $variables['node']->type;
	}
}

On a side note you can actually remove the page title on a specific content type like this. The example below removes the title as well as defining a custom page.tpl.php template.

function themename_preprocess_page(&$variables, $hook) {   
	//Add custom page.tpl.php based on content type 
	if (isset($variables['node']->type) && !empty($variables['node']->type) && ($variables['node']->type == 'my_content_type')){
	// (underscores used in page__ )
	$variables['theme_hook_suggestions'][] =  'page__' .  $variables['node']->type;
	$variables['title'] = '';
	}
}
Justin Chevallier

Justin Chevallier

Avid Drupal site builder & user for +9 years.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.