首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Switch语句不更改WordPress主题选项页中的CSS

Switch语句不更改WordPress主题选项页中的CSS
EN

Stack Overflow用户
提问于 2011-11-15 08:04:03
回答 1查看 342关注 0票数 0

我正在为我的新主题创建一个主题选项页面,并且我设置了一些单选按钮,以便在用户单击每个按钮时更改样式。保存更改后,应该在主题的标题中添加一个样式表。它不能正常工作。我假设我的switch语句可能写得不正确。我使用的是WordPress的设置API。下面是大部分有问题的代码。

代码语言:javascript
复制
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&apos; 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
}

在header主题模板中,我像这样调用样式切换器:

代码语言:javascript
复制
<?php $mxs_settings = get_option('mixinstyles_theme_options');
echo $mxs_settings['mxs_style_switcher']; ?>
EN

回答 1

Stack Overflow用户

发布于 2011-11-21 12:11:50

实际上,我想出了一种方法,不是直接在头文件中调用mxs_style_switcher函数,而是通过add_action标记添加它。在函数中,我取消了global $mixinstyles_theme_options;语句的注释。然后,在函数之外,我将

代码语言:javascript
复制
add_action('wp_head', 'mxs_style_switcher');

最初,我计划直接从主题的头文件中调用该函数,但我决定只使用更可能工作的方法。感谢收看。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8129923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档