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.

>

The output could look like this for example.


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.


Posted

in

by

Comments

11 responses to “Change your WordPress Theme on Dependency”

  1. ovidiu Avatar

    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 Avatar

    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 Avatar

    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. […] ¿Te gustaría que el theme de tu WordPress cambiara dependiendo de el usuario que lo visita? o ¿prefieres que se vea diferente a unas horas determinada? Quizas, prefieras que el theme dependa de la IP del visitante. Si es así, este artículo te puede interesar. […]

  5. […] to WordPress Themes include new functions and body classes, automatic_feed_links, and […]

  6. […] to WordPress Themes include new functions and body classes, automatic_feed_links, and […]

  7. David Avatar

    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!

  8. Alex Avatar

    Hey David,

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

  9. David Avatar
    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!

  10. Frank Avatar

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

  11. Carlos Avatar
    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.