wordpress后台添加多套style文件切换 style switcher

阅读更多

项目需求:

1.添加多套style文件不定期 切换风格

2.后台方便切换管理

 

插件实现

步骤1.

在当前的主题文件下找到functions.php文件 没有的话自己写一个

copy如下代码到文件

$themename = "My theme";
$shortname = "MT";
$options = array (
array( "name" => "Style Sheet",
	"desc" => "Enter the Style Sheet you would like to use for Sweet Ass Theme",
	"id" => $shortname."_style_sheet",
	"type" => "select",
	"options" => array("default", "green", "blue", "yellow"), 
	"std" => "default"),
);

//presentation//

function mytheme_add_admin() {
 
global $themename, $shortname, $options;
 
if ( $_GET['page'] == basename(__FILE__) ) {
 
if ( 'save' == $_REQUEST['action'] ) {
 
foreach ($options as $value) {
update_option( $value['id'], $_REQUEST[ $value['id'] ] ); }
 
foreach ($options as $value) {
if( isset( $_REQUEST[ $value['id'] ] ) ) { update_option( $value['id'], $_REQUEST[ $value['id'] ]  ); } else { delete_option( $value['id'] ); } }
 
header("Location: themes.php?page=functions.php&saved=true");
die;
 
} else if( 'reset' == $_REQUEST['action'] ) {
 
foreach ($options as $value) {
delete_option( $value['id'] ); }
 
header("Location: themes.php?page=functions.php&reset=true");
die;
 
}
}
 
add_theme_page($themename." Options", "".$themename." Options", 'edit_themes', basename(__FILE__), 'mytheme_admin');
 
}
 
function mytheme_admin() {
 
global $themename, $shortname, $options;
 
if ( $_REQUEST['saved'] ) echo '

'.$themename.' settings saved.

'; if ( $_REQUEST['reset'] ) echo '

'.$themename.' settings reset.

'; ?>

Settings


 

 

步骤2

添加如下代码到header.php文件 替换掉原来的style.css加载文件 实现与前台的衔接

 

			
	
		
		
	
		
	
	
		
	
	
		
		
	
?>

 

 

步骤3.

部署文件

将不同套的green.css blue.css yellow.css 文件加到与style.css相同的路径下

 

步骤4.

在后台切换 保存后 查看效果

 

扩展:

前台切换 可以参考

http://www.dynamicdrive.com/dynamicindex9/stylesheetswitcher.htm

 

 

你可能感兴趣的:(wordpress,php,style,css,html)

" />
 
 
 
 
 
 
/>