我正在为我的新主题创建一个主题选项页面,我设置了一些单选按钮,以便在用户单击每个按钮时更改样式。保存更改后,应该将样式表添加到主题的标题中。它无法正常工作。我假设我的switch语句可能写得不正确。我正在使用WordPress的设置API。以下是大部分相关代码。
function mxs_admin_init() {
register_setting(
'mixinstyles_theme_options',
'mixinstyles_theme_options',
'mixinstyles_options_validate'
);
add_settings_section(
'mixinstyles_main',
'Mixin' Styles Settings',
'theming_section_text',
'mixinstyles'
);
add_settings_field(
'custom_style_buttons',
'<strong>Color Schemes</strong>',
'custom_style_buttons',
'mixinstyles',
'mixinstyles_main'
);
}
...
function mxs_theme_options_page() { ?>
<div class="wrap" style="margin-bottom: 20px;">
<div id="icon-themes" class="icon32"><br /></div><h2>Mixin' Styles Theme Options</h2>
<?php if($_REQUEST['settings-updated'] == 'true') {
echo '<div id="message" class="updated fade"><p>Mixin' Styles options saved.</p></div>';
} ?>
<form action="options.php" method="post" name="options_form">
<?php settings_fields('mixinstyles_theme_options'); ?>
<?php do_settings_sections('mixinstyles'); ?>
<div style="text-align: center; padding: 20px;"><input name="Submit" class="button-primary" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" /></div>
</form>
</div>
<?php
}
...
function custom_style_buttons() {
$options = get_option('mixinstyles_theme_options');
echo "<div class='radiobutton-wrap'> \n";
echo "<div class='radiobutton-padding'> \n <input type='radio' id='default_style' value='default_style' name='mixinstyles_theme_options[custom_style_buttons]' /><img src='" . get_bloginfo('template_directory') . "/images/default_screenshot.png' alt='Default style' /><br /><label for='default_style'>Default Style</label> </div> \n";
echo "<div class='radiobutton-padding'> \n <input type='radio' style='padding-left: 10px; padding-right: 10px;' id='blue_orange' value='blue_orange' name='mixinstyles_theme_options[custom_style_buttons]' /><img src='" . get_bloginfo('template_directory') . "/images/blueorange_screenshot.png' alt='Blue/Orange style' /><br /><label for='blue_orange'>Blue/Orange</label> </div> \n";
echo "<div class='radiobutton-padding'> \n <input type='radio' id='violet_yellow' value='violet_yellow' name='mixinstyles_theme_options[custom_style_buttons]' /><img src='" . get_bloginfo('template_directory') . "/images/violetyellow_screenshot.png' alt='Violet/Yellow style' /><br /><label for='violet_yellow'>Violet/Yellow</label> </div> \n";
echo "</div> \n";
echo "<div class='radiobutton-wrap'> \n";
echo "<div class='radiobutton-padding'> \n <input type='radio' id='magenta_green' value='magenta_green' name='mixinstyles_theme_options[custom_style_buttons]' /><img src='" . get_bloginfo('template_directory') . "/images/magentagreen_screenshot.png' alt='Magenta/Green style' /><br /><label for='magenta_green'>Magenta/Green</label></div> \n";
echo "<div class='radiobutton-padding'> \n <input type='radio' id='orange_blue' value='orange_blue' name='mixinstyles_theme_options[custom_style_buttons]' /><img src='" . get_bloginfo('template_directory') . "/images/orangeblue_screenshot.png' alt='Orange/Blue style' /><br /><label for='orange_blue'>Orange/Blue</label></div> \n";
echo "<div class='radiobutton-padding'> \n <input type='radio' id='yellow_violet' value='yellow_violet' name='mixinstyles_theme_options[custom_style_buttons]' /><img src='" . get_bloginfo('template_directory') . "/images/yellowviolet_screenshot.png' alt='Yellow/Violet style' /><br /><label for='yellow_violet'>Yellow/Violet</label></div> \n";
echo "</div> \n";
}
...
function mxs_style_switcher() {
//global $mixinstyles_theme_options;
$options = get_option('mixinstyles_theme_options');
//$blue_orange = $options['blue_orange'];
switch ( $options['custom_style_buttons'] ) { //opens switch statement
case "blue_orange":
echo '"\n" . <link rel="stylesheet" href="';
bloginfo('template_directory');
echo '/custom-styles/blue-orange.css" type="text/css" /> . "\n";';
break;
case "violet_yellow":
echo '"\n" . <link rel="stylesheet" href="';
bloginfo('template_directory');
echo '/custom-styles/violet-yellow.css" type="text/css" /> . "\n";';
break;
case "magenta_green":
echo '"\n" . <link rel="stylesheet" href="';
bloginfo('template_directory');
echo '/custom-styles/magenta-green.css" type="text/css" /> . "\n";';
break;
case "orange_blue":
echo '"\n" . <link rel="stylesheet" href="';
bloginfo('template_directory');
echo '/custom-styles/orange-blue.css" type="text/css" /> . "\n";';
break;
case "yellow_violet":
echo '"\n" . <link rel="stylesheet" href="';
bloginfo('template_directory');
echo '/custom-styles/yellow-violet.css" type="text/css" /> . "\n";';
break;
default:
echo '';
} //closes switch statement
}
在标题主题模板中,我正在调用这样的样式切换器:
<?php $mxs_settings = get_option('mixinstyles_theme_options');
echo $mxs_settings['mxs_style_switcher']; ?>
答案 0 :(得分:0)
我实际上想出了一种方法来实现这项工作,而不是直接调用标题中的mxs_style_switcher
函数,我通过add_action
标记添加了它。在函数中,我取消了global $mixinstyles_theme_options;
语句的注释。然后,在函数之外,我把
add_action('wp_head', 'mxs_style_switcher');
最初,我打算直接从主题的头文件中调用该函数,但我决定只使用更有可能工作的函数。感谢您的观看。