0

This is what I want to accomplish. I've created a taxonomy called categorie with the terms app, web and branding. When a project has the term app, I want to load another theme / blog. When a project has the term web or branding, I want to load single.php. The last one works just fine.

This is my code so far

function load_single_template($template) {
$new_template = '';

if( is_single() ) {
    global $post;

    if( has_term('app', 'categorie', $post) ) {
        $new_template = get_theme_roots('themeApp');
    } else {
        $new_template = locate_template(array('single.php' ));
    }
}
return ('' != $new_template) ? $new_template : $template;
 }
 add_action('template_include', 'load_single_template');

So when a project has the term app, I want to load the theme themeApp. Any suggestions? Thanks in advance.

1
  • I found a workaround. I used the gdHeadSpace plugin which is based on the Head2Space SEO plugin. With the plugin I can refer to another theme in my custom posts.
    – yluijten
    Jan 7, 2014 at 13:14

1 Answer 1

1

Your problem is that if you want to switch theme you have to do it very early, in fqact, the latest action you can hook for that task is setup_theme that is fired before the theme is actually loaded, so - first of all - if you want to change the theme you should use a plugin and not a theme, this is logical: if you have the code to switch theme in the current theme, it should replace itself with another theme and this is not possible.

First reason your code fail: 'template_include' hook its too late.

Second reason: get_theme_roots doesn't take any argument, and returns an array of theme base directories, or single theme directory if all themes are in the same directory (this is the most common case and it returns somting like /path/to/wp/wp-content/themes/). So your use of that function is wrong: hooking 'template_include' you should return a singular file and not the root of themes.

Now, let's admit you use a plugin to switch the current theme, easiest and effective way to do that task is manipulate the 'template' option, using the the 'pre_option_{$option_name}' hook, in this case 'pre_option_template'.

See this answer and replace with 'template' everywhere you see 'stylesheet'.

However, in the linked answer I don't rely on current request to choose the theme, but only on current date that can be calculated very early in the request, because is indepent from it.

In you case, you should check the current request and then switch the theme, but normally WordPress set the theme before run the request, that means that you should

  1. check the request by yourself
  2. switching theme
  3. let WordPress perform the request again

This is overwhelming, require a lot of work and is very low performant.

Of course, you can use 'template_include' filter to load a template file from another theme directory without changing the current theme (probably is what your code is wrote for) but in that case you will have a template file loaded from a theme and stylesheets, functions and scripts loaded from another theme: probably this is not what you want, and in best case it will appear ruined, in worst case it will break the page with a not defined function fatal error.

In conclusion, if you aim is to change apparence of single posts, I suggest to look at a plugin that extend the page template feature to posts, one that I know is this one. then you can copy markup and styles for your single post templates from different themes...

1
  • Thanks a lot for the info G. M. As commented above I'm now using the gdHeadSpace plugin, which is a rather simple way to do what I want to do. But I will definitely take a look at the info for a more ideal solution. Thanks again!
    – yluijten
    Jan 7, 2014 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.