Drupal switch theme by node

Posted by: 
Dominique De Cooman

This article explains how to switch your theme. In our example we will switch the theme if one of the fields of our node has a certain value.
Use hook_init() to accomplish this.

<?php
function thuis_sanitech_extra_init() {
  if (arg(0) == 'node' && is_numeric(arg(1))) {
    $node = node_load(arg(1));
    global $user;
 
    if ($node->field_theme_keuze[0]['value'] == 1) {
      //load appropriate theme
      $user->theme = 'affaires';
      init_theme();
    }
    if ($node->field_theme_keuze[0]['value'] == 0) {
      //load appropriate theme
      $user->theme = 'sanitechniek';
      init_theme();
    }
  }
}
?>

What you need to do is set the global user->theme to the right theme and run init_theme(). That's it.

Comments

Drupal switch theme by node

Thanks Dominique, It's an nice article! However I wonder if we can use the global $custom_theme variable instead of setting $user->theme?
Please check here
http://api.drupal.org/api/global/custom_theme

Thanks,
Andy

Drupal switch theme by node

Using global $custom thema wil cause the themel to change for all users in our case that would be allright too

Add new comment