滤器pre_option_stylesheet
. 未测试的示例代码:
add_filter( \'pre_option_stylesheet\', \'wpse_100854_switch_stylesheet\' );
function wpse_100854_switch_stylesheet( $false )
{
if ( empty ( $_GET[\'theme\'] )
return $false;
$themes = wp_get_themes( array( \'allowed\' => true ) );
if ( isset ( $themes[ $_GET[\'theme\'] ] ) )
return $_GET[\'theme\'];
return $false;
}
URL应如下所示
example.com/?theme=theme_directory_slug
.
要仅更改特定页面的模板,请连接到template_include
:
add_filter( \'template_include\', \'wpse_100854_switch_template\' );
function wpse_100854_switch_template( $template )
{
if ( empty ( $_GET[\'template\'] )
return $false;
// do not allow to break out of the directory
$new = ltrim( $_GET[\'template\'], \'.\' );
$path = get_template_directory() . "/templates/$new.php";
if ( file_exists( $path ) )
return $path;
return $template;
}
现在添加目录
templates
你的主题。当有人要求时
example.com/?template=blue
和模板
blue.php
存在于特殊目录中,将使用它。