简体   繁体   中英

switch wordpress theme base on URL

I have a wordpress website that host several themes inside it. Now I want to switch the themes for visitor base on the URL that they enter. for example :

my WP website is at http://www.myblog.com/wp/

but if user enter :

http://www.myblog.com/wp?theme=twentyten -> using twentyten theme

and if user enter :

http://www.myblog.com/wp?theme=mycustomtheme ->using mycustometheme

then is there any plugins available out there? I've been searching for days but no one seam to work for me.

Any suggestion is very appreciated!

Thanks

I haven't done it, and looking for a minute I don't see a plugin that does that for you. I think your best bet is to make a plugin that would change the theme variable based on the base URL. It's about as simple of a plugin as you can make, so it would be a good one to cut your teeth on if you have any programming ability whatsoever.

I've found this good tutorial about base theme swithing on URL.

First, you have to catch the parameter in the URL:

function sjc_add_query_vars($vars) 
{
    return array('template') + $vars;
}
add_filter('query_vars', 'sjc_add_query_vars');

And then you serve the template:

function sjc_template($template) 
{
    global $wp;
    if ($wp->query_vars['template']=='basic')
    {
        return dirname( __FILE__ ) . '/single-basic.php';
    }
    else 
    {
        return $template;
    }
}
add_filter('single_template', 'sjc_template');

Please note that the code above check if $wp->query_vars['template']=='basic' and not simply return the template specified in the URL, because it could be a security issue.

for who is still interested you have a WordPress functions that switches the theme

switch_theme( $stylesheet )

see https://codex.wordpress.org/Function_Reference/switch_theme

Just be care to call it in the right time, I would write it in a mu-plugin for use it.

This might help: http://codex.wordpress.org/Theme_Switching

There are 2 plugins noted in that url: http://wordpress.org/extend/plugins/theme-preview/ Theme preview which is what you want.

http://wordpress.org/extend/plugins/theme-switcher/ Theme switcher which do that too and create a sidebar widget so user can change the template.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM