2012-01-09 123 views
1

我想检测子域是否为“m”,如果是,则显示不同的模板。找不到我在找的东西,在Wordpress API中这样做。在Wordpress插件中更改模板

+0

如果你想暂时*在活动HTTP请求上使用不同的模板,你需要['template_include' filter](https://codex.wordpress.org/Plugin_API/Filter_Reference/template_include)。如果您想*保留更改,以便将来用于所有请求,则需要直接更新'wp_options'表中的'template'和'stylesheet'选项。请注意['template'和'stylesheet'不相同](https://wordpress.stackexchange.com/a/40220/10388)。 – rinogo 2017-09-26 20:33:49

+0

另请参阅[switch_theme()](https://developer.wordpress.org/reference/functions/switch_theme/)。看起来很多不同的选项。我正在和你们一起解决这个问题! :) – rinogo 2017-09-26 20:54:20

回答

0

可以使用parse_url()函数在PHP中做到这一点:

$parsed = parse_url($_SERVER['HTTP_HOST']); 
$host = explode('.', $parsedUrl['host']); 
$subdomain = $host[0]; 

然后,您可以添加自己的if语句:

if ($subdomain == "m") { ... } else { ... } 
+0

寻找像'wp_switch_template('theme_name');'= 3 – user1139252 2012-01-09 21:31:05

+0

就我所知这并不存在。只需使用上面的IF语句在标题中加载不同的样式表,或者如果它比这更复杂,则可能需要考虑使用WP的网络/多站点功能。 – Justin 2012-01-09 21:49:17

0

您应该使用 'template_redirect' 过滤器,或其中一个模板过滤器用于更改正在加载的模板。

0

我建议创建你自己的插件用一个简单的脚本,并把它的wp-content有这样的代码/插件:

<?php 
/** 
* Plugin Name: Theme swithwer 
* Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates 
* Description: Custom theme switcher 
* Version: 0.1 
* Author: XXX XXX 
* Author URI: http://URI_Of_The_Plugin_Author 
* License: A "Slug" license name e.g. GPL2 
*/ 

add_filter('stylesheet', 'switch_ma_theme'); 
add_filter('template', 'switch_ma_theme'); 

function switch_ma_theme() 

{ 
    global $wpdb; 
    // condition to test ad theme to load 
    if ($_SERVER['HTTP_HOST'] == 'blog.xxxx.xxx') 
     return 'xxxthemexxx'; 
    // else default theme 
    $theme = $wpdb->get_results("SELECT option_value FROM wp_options where option_name='template' "); 
    $theme = array_pop($theme); 
    return $theme->option_value; 
    // Return the theme directory name 

} 

然后在管理走,找你新的插件并启用它