从子域访问时需要切换主题。子域中未安装任何内容。我已将一个子域设置为指向原始域的CNAME。然后,通过查看$\\u服务器[\'SERVER\\u NAME\'],检测访问者是否在子域上。查看了各种来源并发现了此代码,但无法使其正常工作。
注:我是个新手
// regular site url
$my_url = \'http://example.com\';
// subdomain you want a different theme on
$my_sub_url = \'http://sub.example.com\';
// subdomain prefix, used to test whether primary or subdomain
$my_prefix = \'sub.\';
// folder name of theme to use instead on subdomain
$my_theme_name = \'my_mobile_theme\';
add_filter( \'template\' , \'my_change_theme\' );
add_filter( \'option_template\' , \'my_change_theme\' );
add_filter( \'option_stylesheet\' , \'my_change_theme\' );
// these 2 actions rewrite all urls in the body
// from main to sub domain
add_action( \'wp_head\' , \'my_buffer_start\' );
add_action( \'wp_footer\' , \'my_buffer_end\' );
// TRUE, if this is the subdomain, false otherwise
public function my_is_subdomain() {
global $my_prefix;
if( strpos( $_SERVER[\'SERVER_NAME\'], $my_prefix ) !== FALSE ) {
return TRUE;
}
return FALSE;
}
// if this is the subdomain, return new theme
// otherwise, return original theme
public function my_change_theme( $theme ) {
global $my_theme_name;
if( my_is_subdomain() ) {
return $my_theme_name;
}
return $theme;
}
public function my_buffer_start() {
ob_start(\'my_buffer_cb\');
}
public function my_buffer_end() {
ob_end_flush();
}
// replace primary url with subdomain url in body
// so that all links keep user on the subdomain
public function my_buffer_cb( $buffer ) {
global $my__url, $my_sub_url;
if( ! my_is_subdomain() ) {
return $buffer;
}
// replace main domain with sub domain
$buffer = str_replace( $my_url, $my_sub_url, $buffer);
// !!! NOTE - you may not want to replace EVERY instance
// for example, you may want to keep social media urls
// intact, or rel="canonical", or links specifically
// designed to switch between the primary and subdomain
return $buffer;
}