2012-01-09 91 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 

} 

然後在管理走,找你新的插件並啓用它