3

This is what I want to accomplish. In Wordpress 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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
yluijten
  • 123
  • 2
  • 8
  • I found a workaround. I used the [gdHeadSpace](http://www.dev4press.com/2011/blog/plugins-news/gdheadspace4-4-0-3/) plugin which is based on the Head2Space SEO plugin. With the plugin I can refer to another theme in my custom posts. – yluijten Jan 07 '14 at 13:13

1 Answers1

1

We had to accomplish a similar task in our plugin, AppPresser. You can see our solution here: https://github.com/WebDevStudios/AppPresser/blob/master/inc/theme-switcher.php

Basically, you need to change the theme name in 3 filters: 'template', 'option_template', 'option_stylesheet'.

Getting the category is not so simple though, because the template check happens early enough in the WordPress process that the global $post and $wp_query objects are not available.

Here is one way that can be accomplished:

<?php
add_action( 'setup_theme', 'maybe_theme_switch', 10000 );
function maybe_theme_switch() {

    // Not on admin
    if ( is_admin() )
        return;

    $taxonomy = 'category';
    $term_slug_to_check = 'uncategorized';
    $post_type = 'post';

    // This is one way to check if we're on a category archive page
    if ( false !== stripos( $_SERVER['REQUEST_URI'], $taxonomy ) ) {
        // Remove the taxonomy and directory slashes and it SHOULD leave us with just the term slug
        $term_slug = str_ireplace( array( '/', $taxonomy ), '', $_SERVER['REQUEST_URI'] );

        // If the term slug matches the one we're checking, do our switch
        if ( $term_slug == $term_slug_to_check ) {
            return yes_do_theme_switch();
        }
    }

    // Try to get post slug from the URL since the global $post object isn't available this early
    $post = get_page_by_path( $_SERVER['REQUEST_URI'], OBJECT, $post_type );
    if ( ! $post )
        return;

    // Get the post's categories
    $cats = get_the_terms( $post, $taxonomy );
    if ( ! $cats )
        return;

    // filter out just the category slugs
    $term_slugs = wp_list_pluck( $cats, 'slug' );
    if ( ! $term_slugs )
        return;

    // Check if our category to check is there
    $is_app_category = in_array( $term_slug_to_check, $term_slugs );
    if ( ! $is_app_category )
        return;

    yes_do_theme_switch();

}

function yes_do_theme_switch( $template ) {
    // Ok, switch the current theme.
    add_filter( 'template', 'switch_to_my_app_theme' );
    add_filter( 'option_template', 'switch_to_my_app_theme' );
    add_filter( 'option_stylesheet', 'switch_to_my_app_theme' );
}

function switch_to_my_app_theme( $template ) {
    // Your theme slug
    $template = 'your-app-theme';
    return $template;
}
  • Thanks a lot for the info [Justin Sternberg](http://stackoverflow.com/users/1883421/justin-sternberg). As commented above I'm now using the [gdHeadSpace](http://www.dev4press.com/2011/blog/plugins-news/gdheadspace4-4-0-3/) 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 08 '14 at 16:39