一些插件使用\'template\'
, \'option_template\'
和\'option_stylesheet\'
动态提供(可选)wordpress模板。例如,Nathan Rice的ServeDefaultToIESix
. 例如-
add_filter(\'template\', \'change_theme\');
add_filter(\'option_template\', \'change_theme\');
add_filter(\'option_stylesheet\', \'change_theme\');
function change_theme()
{
// Alternate theme
return \'AwesomeTheme\';
}
以上代码仅适用于wordpress插件。我需要的是切换到替代模板,
located in one of the subfolders 当前主题(模板)的。示例:显示替代HTML5主题,为移动用户提供站点的最低版本。。等
我尝试使用“theme\\u root”和“theme\\u root\\u uri”,如下所示。但这是行不通的。
// Extra lines to change the theme\'s root.
add_filter(\'theme_root\', \'change_theme_root\');
add_filter(\'theme_root\', \'change_theme_root_uri\');
//
add_filter(\'template\', \'change_theme\');
add_filter(\'option_template\', \'change_theme\');
add_filter(\'option_stylesheet\', \'change_theme\');
function change_theme()
{
// Display Alternate theme
return \'AwesomeTheme\';
}
function change_theme_root()
{
// Return the new theme root
return WP_CONTENT_DIR . \'themes/OrigTheme/lib/AltThemes\';
}
function change_theme_root_uri()
{
// Return the new theme root uri
return get_bloginfo(\'wpurl\') . \'/wp-content/themes/OrigTheme/lib/AltThemes\';
}
这是正确的做法吗?或者有人知道这样做的可能方式吗?提前谢谢。
最合适的回答,由SO网友:kaiser 整理而成
You could also write your own simple get_template_part alias function:
下面为位于名为“设备”的主题根文件夹中的模板部件提供了3个子文件夹。
<?php
// STYLESHEETS
function print_device_styles( $client = \'desktop\' )
{
$client = apply_filters( \'set_theme_client\', $client );
wp_enqueue_style( $client.\'-css\' );
}
add_action( \'wp_head\', \'print_device_styles\', 11 );
// TEMPLATE PARTS
function get_device_template_part( $args = array( \'file\' => NULL, \'suffix\' => \'default\', \'client\' => \'desktop\', \'media\' => \'screen\' )
{
if ( ! $args[\'file\'] )
wp_die( sprintf( __(\'You have to specify a file name if you want to load a template part with the %1$s function.\', \'textdomain\', \'<pre>get_device_template_part()</pre>\' );
$template_path = user_trailingslashit( get_stylesheet_directory().\'/devices/templates-\'.$args[\'client\'] );
$ui_path = user_trailingslashit( get_stylesheet_directory().\'/ui/css-\'.$args[\'client\'] );
$ui_suffix = \'.css\'; // could be switched between \'.dev.css\' & \'.css\'
// add styles & template directory
if ( is_condition_mobile() )
{
$args[\'client\'] = \'mobile\';
$args[\'screen\'] = \'handheld\';
}
elseif ( is_condition_tablet() )
{
$args[\'client\'] = \'tablet\';
$args[\'screen\'] = \'handheld\';
}
// register styles
// wp_register_style( \'mobile-css\', /theme_root/ui/css-mobile/mobile.css, false \'handheld\' );
wp_register_style( $args[\'client\'].\'-css\', $ui_path.$args[\'client\'].$ui_suffix, false, $args[\'screen\'] );
// Requires PHP 5.3+ (for lower versions, use a plain function).
add_filter( \'set_theme_client\', function(\'set_theme_client\') { return $args[\'client\'];} );
// {$template}-{$suffix}.php
if ( file_exists( $template_path.$args[\'file\'].\'-\'.$args[\'suffix\'].\'.php\' ) )
{
require( $template_path.$args[\'file\'].\'-\'.$args[\'suffix\'].\'php\' );
}
// {$template}.php
elseif ( file_exists( $template_path.$args[\'file\'].\'.php\' ) )
{
require( $template_path.$args[\'file\'].\'.php\' );
}
// {$template}-default.php
elseif ( file_exists( $template_path.$args[\'file\'].\'-default.php\' ) )
{
require( $template_path.$args[\'file\'].\'-default.php\' );
}
}
// CALL THEM in a template file
// This will require a file named {$template}-{$suffix}.php from your devices folder
// based on your conditional functions that detect your devices
// If not found, it will search for a file named {$template}.php
// and if it wasn\'t found it will search for a file named {$template}-default.php
get_device_template_part( array( \'file\' => \'nav\', \'suffix\' => \'main\' ) );
?>
您可以将有条件的设备检测添加到:
https://gist.github.com/886501
SO网友:kaiser
通常插件和主题具有相同的可能性。但在某些方面,有很大的不同。主题出现较晚,因此影响力较小。另外:很多功能只适用于插件。
您试图做的是修改核心行为。在某些地方,core提供了钩子,甚至可以完全交换部分,而在其他地方,它提供了钩子来扩展其功能。但请记住,核心从来都不希望在涉及主题时真正改变其行为(请参见子文件夹和get_template_part()
). 因此,我不再“在”主题中切换主题(就像修改主查询),而是寻找其他方法:
// inside one of your template files:
do_action( \'main_nav_hook\' );
// inside your functions.php
define( \'THEME_DESKTOP_DIR\', user_trailingslashit( get_stylesheet_directory().\'/desktop\' ) );
define( \'THEME_MOBILE_DIR\', user_trailingslashit( get_stylesheet_directory().\'/mobile\' ) );
define( \'THEME_TABLET_DIR\', user_trailingslashit( get_stylesheet_directory().\'/tablett\' ) );
function add_main_nav_desktop()
{
// do stuff
require_once( THEME_DESKTOP_DIR.\'template_nav.php\' );
}
function add_main_nav_mobile()
{
// do stuff
require_once( THEME_MOBILE_DIR.\'template_nav.php\' );
}
function add_main_nav_tablett()
{
// do stuff
require_once( THEME_TABLET_DIR.\'template_nav.php\' );
}
// Hook the actions
if ( is_condition_mobile() )
{
add_action( \'main_nav_hook\', \'add_main_nav_mobile\' );
}
elseif ( is_condition_tablet() )
{
add_action( \'main_nav_hook\', \'add_main_nav_tablet\' );
}
else
{
add_action( \'main_nav_hook\', \'add_main_nav_desktop\' );
}
这是一种非常简单和基本的方法,但更有可能奏效。