2011-11-15 14 views
0

我爲我的新主題創建主題選項頁面,並且我設置了一些單選按鈕以在用戶單擊每個按鈕時更改樣式。保存更改後,應該將樣式表添加到主題的標題中。它工作不正常。我假設我的switch語句可能寫錯了。我正在使用WordPress的設置API。下面是大多數有問題的代碼。切換語句不改變WordPress主題選項頁面中的CSS

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 
} 

在頭的主題模板,我打電話的風格切換是這樣的:

<?php $mxs_settings = get_option('mixinstyles_theme_options'); 
echo $mxs_settings['mxs_style_switcher']; ?> 

回答

0

我居然想出了一個辦法通過,使這項工作,而不是調用mxs_style_switcher功能在頭的直接,我通過add_action標籤添加它。在該函數中,我取消了註釋global $mixinstyles_theme_options;聲明。然後,功能之外,我把

add_action('wp_head', 'mxs_style_switcher'); 

本來,我打算直接從主題的頭文件調用的函數,但我決定只使用什麼是更容易合作。感謝您的觀看。

相關問題