Drupal Page Theme According to Content Type

I have used this a lot in the past for most of my Drupal projects – To theme a page in Drupal (v. 6) according to its content-type (ie page-news.tpl.php) . . . you will need this piece of code in template.php – it also makes a lot of other suggestions including getting the page url alias . . .


function themename_preprocess_page(&$variables) {
global $user;
if ($node = menu_get_object()) {
$variables['node'] = $node;
$suggestions = array();
$template_filename = 'page';
$template_filename = $template_filename . '-' . $variables['node']->type;
$suggestions[] = $template_filename;
$variables['template_files'] = $suggestions;
// Add a single suggestion.
if (module_invoke('throttle', 'status') && isset($user->roles[1])) {
$variables['template_file'] = 'page-busy';
}
// Add multiple suggestions.
if ($variables['node']->type == "page") {
$alias = drupal_get_path_alias($_GET['q']);
if ($alias != $_GET['q']) {
$template_filename = 'page';
foreach (explode('/', $alias) as $path_part) {
$template_filename = $template_filename . '-' . $path_part;
$variables['template_files'][] = $template_filename;
}
}
}
if (!empty($user->roles)) {
foreach ($user->roles as $role) {
$filter = '![^abcdefghijklmnopqrstuvwxyz0-9-_]+!s';
$string_clean = preg_replace($filter, '-', drupal_strtolower($role));
$variables['template_files'][] = 'page-'. $string_clean;
}
if (drupal_is_front_page()) {
$variables['template_file'] = 'page-front';
}
}
}
}

Tags: , ,

Leave a Reply