Dominique De Cooman
Published on Dominique De Cooman (https://dominiquedecooman.com)

Home > Drupal switch theme by node

Dominique De Cooman
Wednesday, October 1, 2008 - 23:01
theming [1]

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() [2] 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.


Source URL: https://dominiquedecooman.com/blog/drupal-switch-theme-node

Links
[1] https://dominiquedecooman.com/blog-topics/theming
[2] http://api.drupal.org/api/function/hook_init/5