Change your WordPress Theme on Dependency

WordPress is sometimes not only in use as a blog platform, also as CMS application or for a presentation of photos or other purposes. Many times you like to have a different design depending on the purpose of your WordPress blog within your website. Thus, most of the photo blogs having a different design than the weblog.

In fact you can achieve that quite easily and do not always need to create an extensive query in your template or even a different installation of WordPress setup. You can also deal with the following solution, giving you a better overview of the separate themes. With this solution you have many possibilities to implement individual designs.

wptheme_change

With WordPress 2.8

In the coming version of WordPress you can find the new template tag - body_class(), which assigns several classes to the body-Tag, this will support the individuality. More information provides Michael, inclusive all possible classes. Alternative you can read more about it on Andrew's Website, who already analyzed this new function.

<body <?php body_class(); ?>>

The output could look like this for example.

<body class="page-template page-template-tutorial-php logged-in">

Everything different

But you don't have to wait for WordPress 2.8, there is already a nice and clean solution available, at least in my opinion. I separate the themes if there is a big difference between them, for example between my weblog and photoblog. In this case I make a query for categorie, which is only for photos and activate another theme for the frontend.

With this basic idea there is a lot potential to use it in many cases. I listed some examples of applications, which I use in my projects. Check out which one fit your needs.

To use this possibility you can either put the function in functions.php of your theme, which is active in your backend of WordPress, or you save it as a Plugin. I created a little Plugin for that, which you can only change in the code directly, there is no user interface in your admin available. Here is the page of the Plugin „User Theme“.

function fb_user_theme( $template = '' ) {

	// en_US: replace space in the name of a theme-folder with _ (underline)!

	// when Profil-ID
	if ( get_profile('ID') == 123 ) {
		$template = 'default';
	}

	// when IP
	if ( $_SERVER['REMOTE_ADDR'] == '127.0.0815.1' ) {
		$template = 'classic';
	}
	
	// when User_Level
	// @see http://codex.wordpress.org/Roles_and_Capabilities#Roles
	if ( current_user_can('level_10') ) {
		$template = 'classic';
	}

	elseif ( current_user_can('level_5') ) {
		$template = 'classic';
	}
	
	elseif ( current_user_can('level_2') ) {
		$template = 'default';
	}
	
	// when category with ID
	if ( in_category('1') ) {
		$template = 'default';
	}
	
	return $template;
}

add_filter('template', 'fb_user_theme');
add_filter('stylesheet', 'fb_user_theme');

In fact it is quite simple: if a condition is true, for example, the category with ID 1, then the default theme gets loaded, otherwise always the theme gets loaded, which is active in the backend.
In the function the name of the theme gets delivered to the variable $template and with the help of the two filters the template and the stylesheet to the given theme name will be activated.

The name of the theme can have a space, which has to be replaced in the function with _ (underline), please don't forget, otherwise WordPress cannot recognize the theme.

With this small and simple way you can save a lot of detours, summarize installations of WordPress and still retains a clear separation of the themes. The function can, of course, be used for all types of conditional tags, it is also conceivable that static pages, or posts with certain tags can have a different design.

Comments are closed.

11 comments

  1. ovidiu

    this plugin is very handy and similar to the new functionality of wp 2.8: http://www.alistercameron.com/2007/01/04/wordpress-plugin-classybody/

  2. Christopher Ross

    Hi Frank, thanks for pointing out one of the great new features in 2.8! I'm not sure how practical it will be for most people but with luck, we'll see some wonderful new themes using it.

  3. Mike

    If in wp_header you load from to
    then you could put in each template file beit page/single/archive etc etc

    No conditionals needed that aren't already done by the CMS...

  4. David

    Hi Frank,
    thanks for the plugin. I wonder how can I do to change the theme for just a single page. I have a forum on my blog and I want it to have a different theme than the rest of the site, can it be done easily?

    thanks a lot!

  5. Alex

    Hey David,

    yes, that's what the Plugin is exactly for. 🙂

  6. David

    Hey Alex,
    thanks, I may be missing something then :-), I do not understand yet how can I then assign a specific theme to one single page of my site. It is not based on category, user level or IP, just everyone going to a specific page to get a different theme than the rest.

    thanks again for the support!

  7. Frank

    @David: for single-page you ask with a conditional tag: is_single(). Thats all. Please see the codex of this tag for more informations.

  8. Carlos

    Hi all (long time ago),

    I'm trying to change the entire theme depending on categories.
    If category is A then I must use /themes/themeA
    else I must use the current theme /themes/themeB
    I mean, my wP theme is always themeB except if I'm showing category A.

    I've tried to use your pluging but with no luck. I can change the theme depending on categories but I can't load the style.css (and other needed files) from the themeA folder.

    I think that the key is in the add_filter function but I'm not a good developer.
    How can I indicate that if have to load themeA then I have to load the full teme from a different folder?

    Running WP 3.01

    Thanks in advance.

3 pingbacks

  1. conditional_theme(), un theme cada situación | aNieto2K
  2. WordPress News: WordPress 2.8, Summer of Code, PodPress Returning, iPhone Update | The Blog Herald
  3. WordPress News: Widget API, Bye-Bye Kubrick, Query_Posts Plugin, and Template Tag Plugin « BLOGCHINA.ME