We use cookies to improve our services and provide a better experience. By continuing to use this site, you agree with our Cookies Policy.

Tutorials
More than 400 step-by-step articles to guide you through online project development.
HomeTutorialsCMS TutorialsDrupal

How to add a custom page.tpl based on content types

Germany Cloud Servers
Brazil VPS
OpenVZ VPS
USA VPS
KVM VPS
Ubuntu VPS
Windows Cloud Server
Linux KVM Hosting
cPanel KVM
Linux Cloud Server

While it’s possible to add custom page.tpl based on content types in Drupal 6 by just creating file “page-(content type name)-tpl.php, unfortunately, it’s no longer in Drupal 7. Therefore, if we would like to specify content type with page.tpl file in Drupal 7, we’ll need to implement additional code entry for the template file.

Here we will provide an example code which should be used in the file named “template.php”. It can be found in the “themes” directory of currently enabled theme sub-folder.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
function theme_name_preprocess_page(&amp;$vars) {
 
if (isset($vars['node'])) {
 
$suggests = &amp;$vars['theme_hook_suggestions'];
 
$args = arg();
unset($args[0]);
 
$type = "page__type_{$vars['node']-&gt;type}";
 
$suggests = array_merge(
$suggests,
array($type),
theme_get_suggestions($args, $type)
);
}
}
?>

Before using this code, make sure to replace “theme_name” with the actual theme name. This should be working with all content types and even with custom template for website editing. However, this is just one of the examples how this feature can be used for creating custom page.tpl based on content types in Drupal 7. It should be possible to do similar thing in many different methods.

Linux Cloud Server
cPanel KVM
OpenVZ VPS
Windows Cloud Server
USA VPS
Linux KVM Hosting
Ubuntu VPS
Germany Cloud Servers
KVM VPS
Brazil VPS
Rate this Tutorial:
No Comments Leave a Comment
Leave a Comment

Other (195)

Popular Keywords