主题选项页面-选择、单选、选项卡

时间:2012-06-06 作者:Arg Geo

下面的代码是我的主题选项页面(“外观”下的子菜单)。除“select”(选择)和“radio”(收音机)外,所有选项均正常工作。。。它们不会像其他选项那样显示在页面中。我已经在互联网上搜索了三天,尝试了不同的解决方案,但没有办法让它们出现。之后,我想在不同的选项卡中分离部分,这也是我无法做到的。我已经阅读了相关文档和我找到的一些教程,但还没有任何东西能起到作用。。。有什么帮助吗?谢谢

    <?php
$themename = "WIS";

$shortname = "wis";

$version = "1.1";

$option_group = $shortname.\'_theme_option_group\';
$option_name = $shortname.\'_theme_options\';


add_action(\'admin_init\', \'wis_add_init\');

function wis_add_init() {
    $file_dir = get_template_directory_uri();

    wp_enqueue_style("wisCss", $file_dir."/functions/theme-options.css", false, "1.0", "all");

    wp_enqueue_script("wisScript", $file_dir."/functions/theme-options.js", false, "1.0");

}

// Create custom settings menu
add_action(\'admin_menu\', \'wis_create_menu\');

function wis_create_menu() {
    global $themename;
    //create new top-level menu
    add_theme_page( __( $themename.\' Theme Options\' ), __( \'Theme Options\' ), \'edit_theme_options\', \'wisgentheoptpag\', \'wis_settings_page\' );
}

// Register settings
add_action( \'admin_init\', \'register_settings\' );

function register_settings() {
   global $themename, $shortname, $version, $wis_options, $option_group, $option_name;
    //register our settings
    register_setting( $option_group, $option_name);
}

// Create theme options

global $wis_options;

$wis_options = array (

array("name" => __(\'RSS Feeds/Facebook/Twitter\',\'wis\'),

        "type" => "section"),

array("name" => __(\'Set up social links.\',\'wis\'),

        "type" => "section-desc"),

array("type" => "open"),

array("name" => __(\'Custom Feed URL\',\'wis\'),

        "desc" => __(\'You can use your own feed URL (<strong>with http://</strong>). Paste your Feedburner URL here to let readers see it in your website.\',\'wis\'),

        "id" => "feedurl",

        "type" => "text",

        "std" => get_bloginfo(\'rss2_url\')),

array("name" => __(\'Delete Extra Feeds\',\'wis\'),

        "desc" => __(\'WordPress adds feeds for categories, tags, etc., by default. Check this box to remove them and reduce the clutter.\',\'wis\'),

        "id" => "cleanfeedurls",

        "type" => "checkbox",

        "std" => ""),

array("name" => __(\'Twitter ID\',\'wis\'),

        "desc" => __(\'Your Twitter user name, please. It will be shown in the navigation bar. Leaving it blank will keep the Twitter icon supressed.\',\'wis\'),

        "id" => "twitterid",

        "type" => "text",

        "std" => ""),

array("name" => __(\'Facebook Page\',\'wis\'),

        "desc" => __(\'Link to your Facebook page, <strong>with http://</strong>. It will be shown in the navigation bar. Leaving it blank will keep the Facebook icon supressed.\',\'wis\'),

        "id" => "facebookid",

        "type" => "text",

        "std" => ""),

array("type" => "close"),

//FOOTER

array("name" => __(\'Footer\',\'wis\'),

        "type" => "section"),

array("name" => __(\'Customize footer of your website.\',\'wis\'),

        "type" => "section-desc"),

array("type" =>"open"),

array("name" => __(\'Footer Text\',\'wis\'),

        "desc" => __(\'Enter your footer text or HTML here.\',\'wis\'),

        "id" => "footer_text",

        "std" => "",

        "type" => "textarea"),

array("name" => __(\'Hide Footer Navigation Links\',\'wis\'),

        "desc" => __(\'Select to hide the navigation bar in the footer. If you want to customize the footer navigation, go to Menus under the Appearance tab in the dashboard.\',\'wis\'),

        "id" => "hide_footer_nav",

        "std" => "",

        "type" => "checkbox"),

array("name" => __(\'Hide Footer Credits\',\'wis\'),

        "desc" => __(\'Select to hide the credit line in the footer. Thanks for sending us a <strong>generous contribution</strong>.\',\'wis\'),

        "id" => "hide_footer_credit",

array("name" => __(\'Theme Styles\',\'wis\'),

        "type" => "section"),

array("name" => __(\'Choose a color scheme and add custom CSS styles.\',\'wis\'),

        "type" => "section-desc"),

array("type" => "open"),

array("name" => __(\'Colour Scheme\',\'wis\'),

        "desc" => __(\'Select a colour scheme for the theme. Future versions will have multiple styles.\',\'wis\'),

        "id" => "alt_stylesheet",

        "type" => "select",

        "options" => $alt_stylesheets,

        "std" => "default.css"),

array( "name" => __(\'Custom Styles\',\'wis\'),

    "desc" => __(\'Want to add any custom CSS code? Put in here, and the rest is taken care of. This overrides any other stylesheets. eg: a.button{color:green}\',\'wis\'),

    "id" => "custom_css",

    "type" => "textarea",

    "std" => ""),

array("type" => "close"),

        "std" => "",

        "type" => "checkbox"),

array( "type" => "close"),

//ADVERTISEMENTS --- POST ADS

array("name" => __(\'Advertisements\',\'wis\'),

    "type" => "section"),

array("name" => __(\'Show ads on your blog.\',\'wis\'),

        "type" => "section-desc"),

array("type" => "open"),

array("name" => __(\'Header Display Ad\',\'wis\'),

        "desc" => __(\'You can show a display ad in header. Paste the code here for 600px by 60px ad.\',\'wis\'),

        "id" => "topbanner",

        "std" => "",

        "type" => "textarea"),

array("name" => __(\'Ad Above Posts\',\'wis\'),

        "desc" => __(\'Enter your Adsense code or other ad network code here. This ad will be displayed at the beginning of posts, below title on Post Pages and Pages with ad-supporting template. It is very basic and effective option for putting ads on your blog. If you want more functionality, get a specialized Ad plugin.\',\'wis\'),

        "id" => "posttop_adcode",

        "std" => "",

        "type" => "textarea"),

array("name" => __(\'Ad Below Posts\',\'wis\'),

        "desc" => __(\'Enter your Adsense code (or other ad network code) here. This ad will be displayed at the end of post content on Post Pages and Pages with ad-supporting template. Please make sure that you do not activate more ads than what is allowed by your ad network. Adsense allows up to 3 on one page.\',\'wis\'),

        "id" => "postend_adcode",

        "std" => "",

        "type" => "textarea"),

array("type" => "close"),

//Analytics Code

array("name" => __(\'Tracking & Other Codes\',\'wis\'),

array("name" => __(\'Theme Styles\',\'wis\'),

        "type" => "section"),

array("name" => __(\'Choose a color scheme and add custom CSS styles.\',\'wis\'),

        "type" => "section-desc"),

array("type" => "open"),

array("name" => __(\'Colour Scheme\',\'wis\'),

        "desc" => __(\'Select a colour scheme for the theme. Future versions will have multiple styles.\',\'wis\'),

        "id" => "alt_stylesheet",

        "type" => "select",

        "options" => $alt_stylesheets,

        "std" => "default.css"),

array( "name" => __(\'Custom Styles\',\'wis\'),

    "desc" => __(\'Want to add any custom CSS code? Put in here, and the rest is taken care of. This overrides any other stylesheets. eg: a.button{color:green}\',\'wis\'),

    "id" => "custom_css",

    "type" => "textarea",

    "std" => ""),

array("type" => "close"),


        "type" => "section"),

array("name" => __(\'Insert Web tracking & analytics and other codes here.\',\'wis\'),

        "type" => "section-desc"),

array("type" => "open"),

array("name" => __(\'Analytics & Tracking Code\',\'wis\'),

        "desc" => __(\'You can paste your Google Analytics or other codes in this box. The codes will be automatically added to the footer.\',\'wis\'),

        "id" => "analytics_code",

        "type" => "textarea",

        "std" => ""),

array("type" => "close")

);

function wis_settings_page() {
   global $themename, $shortname, $version, $wis_options, $option_group, $option_name;
?>

<div class="wrap">
<div class="options_wrap">
<?php screen_icon(); ?><h2><?php echo $themename; ?> <?php _e(\'Theme Options\',\'wis\'); ?></h2>
<p class="top-notice"><?php _e(\'Customize your WordPress blog with these settings. \',\'wis\'); ?></p>
<?php if ( isset ( $_POST[\'reset\'] ) ): ?>
<?php // Delete Settings
global $wpdb, $themename, $shortname, $version, $wis_options, $option_group, $option_name;
delete_option(\'wis_theme_options\');
wp_cache_flush(); ?>
<div class="updated fade"><p><strong><?php _e( $themename. \' options reset.\' ); ?></strong></p></div>

<?php elseif ( isset ( $_REQUEST[\'updated\'] ) ): ?>
<div class="updated fade"><p><strong><?php _e( $themename. \' options saved.\' ); ?></strong></p></div>
<?php endif; ?>

<form method="post" action="options.php">

<?php settings_fields( $option_group ); ?>

<?php $options = get_option( $option_name ); ?>


<?php foreach ($wis_options as $value) {
if ( isset($value[\'id\']) ) { $valueid = $value[\'id\'];}

switch ( $value[\'type\'] ) {

case "section":

?>

    <div class="section_wrap">

    <h3 class="section_title"><?php echo $value[\'name\']; ?>

<?php break;

case "section-desc":

?>

    <span><?php echo $value[\'name\']; ?></span></h3>

    <div class="section_body">

<?php

break;

case \'text\':

?>

    <div class="options_input options_text">

        <div class="options_desc"><?php echo $value[\'desc\']; ?></div>

        <span class="labels"><label for="<?php echo $option_name.\'[\'.$valueid.\']\'; ?>"><?php echo $value[\'name\']; ?></label></span>

        <input name="<?php echo $option_name.\'[\'.$valueid.\']\'; ?>" id="<?php echo $option_name.\'[\'.$valueid.\']\'; ?>" type="<?php echo $value[\'type\']; ?>" value="<?php if ( isset( $options[$valueid]) ){ esc_attr_e($options[$valueid]); } else { esc_attr_e($value[\'std\']); } ?>" />

    </div>

<?php

break;

case \'textarea\':

?>

    <div class="options_input options_textarea">

        <div class="options_desc"><?php echo $value[\'desc\']; ?></div>

        <span class="labels"><label for="<?php echo $option_name.\'[\'.$valueid.\']\'; ?>"><?php echo $value[\'name\']; ?></label></span>

        <textarea name="<?php echo $option_name.\'[\'.$valueid.\']\'; ?>" type="<?php echo $option_name.\'[\'.$valueid.\']\'; ?>" cols="" rows=""><?php if ( isset( $options[$valueid]) ){ esc_attr_e($options[$valueid]); } else { esc_attr_e($value[\'std\']); } ?></textarea>

    </div>

<?php

break;

case \'select\':

?>

    <div class="options_input options_select">

        <div class="options_desc"><?php echo $value[\'desc\']; ?></div>

        <span class="labels"><label for="<?php echo $option_name.\'[\'.$valueid.\']\'; ?>"><?php echo $value[\'name\']; ?></label></span>

        <select name="<?php echo $option_name.\'[\'.$valueid.\']\'; ?>" id="<?php echo $option_name.\'[\'.$valueid.\']\'; ?>">

        <?php foreach ($value[\'options\'] as $option) { ?>

                <option <?php if ($options[$valueid] == $option) { echo \'selected="selected"\'; } ?>><?php echo $option; ?></option><?php } ?>

        </select>

    </div>

<?php

break;

case "radio":

?>

    <div class="options_input options_select">

        <div class="options_desc"><?php echo $value[\'desc\']; ?></div>

        <span class="labels"><label for="<?php echo $option_name.\'[\'.$valueid.\']\'; ?>"><?php echo $value[\'name\']; ?></label></span>

          <?php foreach ($value[\'options\'] as $key=>$option) {
            if ( isset ( $options[$valueid] ) ) {
                if ($key == $options[$valueid] ) {
                    $checked = "checked=\\"checked\\"";
                    } else {
                        $checked = "";
                    }
            }else{
                if($key == $value[\'std\']){
                    $checked = "checked=\\"checked\\"";
                }else{
                    $checked = "";
                }
            }?>

            <input type="radio" id="<?php echo $option_name.\'[\'.$valueid.\']\'; ?>" name="<?php echo $option_name.\'[\'.$valueid.\']\'; ?>" value="<?php echo $key; ?>" <?php echo $checked; ?> /><?php echo $option; ?><br />

            <?php } ?>

    </div>

<?php

break;

case "checkbox":

?>

    <div class="options_input options_checkbox">

        <div class="options_desc"><?php echo $value[\'desc\']; ?></div>

        <?php if( isset( $options[$valueid] ) ){ $checked = "checked=\\"checked\\""; }else{ $checked = "";} ?>

        <input type="checkbox" name="<?php echo $option_name.\'[\'.$valueid.\']\'; ?>" id="<?php echo $option_name.\'[\'.$valueid.\']\'; ?>" value="true" <?php echo $checked; ?> />

        <label for="<?php echo $option_name.\'[\'.$valueid.\']\'; ?>"><?php echo $value[\'name\']; ?></label>

     </div>

<?php

break;

case "close":

?>

</div><!--#section_body-->

</div><!--#section_wrap-->

<?php
break;

}

}

?>

<span class="submit">
<input class="button button-primary" type="submit" name="save" value="<?php _e(\'Save All Changes\', \'wis\') ?>" />
</span>
</form>

<form method="post" action="">

<span class="button-right" class="submit">

<input class="button button-secondary" type="submit" name="reset" value="<?php _e(\'Reset\', \'wis\') ?>" />
<input type="hidden" name="action" value="reset" />
<span id="caution"><?php _e(\'<span id="emph">Caution: All entries will be deleted from database.</span>\',\'wis\') ?></span>

</span>

</form>
</div><!--#options-wrap-->
</div>

<?php } ?>

1 个回复
最合适的回答,由SO网友:pixelngrain 整理而成

根据我的经验,isset不适用于文本、文本区域,它适用于复选框、收音机等(可能有人可以为此提供深入指导)

要使用下拉选择设置选项,我更喜欢使用开关和大小写,如下所示

<?php
    switch (get_option(\'your_option_id\')) {

                case "Default": ?>
                <link rel="stylesheet" href="<?php bloginfo(\'template_url\'); ?>/css/default.css" media="screen" />
                <?php break;

                case "Red": ?>
                <link rel="stylesheet" href="<?php bloginfo(\'template_url\'); ?>/css/red.css" media="screen" />
                <?php break;

                case "Blue": ?>
                <link rel="stylesheet" href="<?php bloginfo(\'template_url\'); ?>/css/blue.css" media="screen" />
                <?php break;

                case "Green": ?>
                <link rel="stylesheet" href="<?php bloginfo(\'template_url\'); ?>/css/green.css" media="screen" />
                <?php break;

                case "Yellow": ?>
                <link rel="stylesheet" href="<?php bloginfo(\'template_url\'); ?>/css/yellow.css" media="screen" />
                <?php break;

                case "Black": ?>
                <link rel="stylesheet" href="<?php bloginfo(\'template_url\'); ?>/css/black.css" media="screen" />
                <?php break;

            }
?>
这可以直接放在标题中,我们在这里调用样式表,也可以为此创建函数。我通常创建函数,这样主题文件就不会变得太大。

要检查isset,您可以使用!=对于文本,这将不会有任何问题,所以可以使用下面的内容。

<?php
    if(get_option(\'myfeed_url\') != \'\') {

        //do this (place your code here)

    }
?>
正如我在上面一行所说的。您可以直接将代码放置到适当的位置(在您的header.php选项卡中可能是),或者可以在函数中创建函数。然后,您必须在适当的位置调用该函数。

希望这就是你要找的

---[ Use This Tested Code ]-------------------------------------------------

<?php

$themename = "WIS";
$shortname = "wis";

$categories = get_categories(\'hide_empty=0&orderby=name\');
$wp_cats = array();
foreach ($categories as $category_list ) {
       $wp_cats[$category_list->cat_ID] = $category_list->cat_name;
}
array_unshift($wp_cats, "Choose a category"); 

// your stylesheet selection options
//$alt_stylesheets = array("default.css" => "Default", "blue.css" => "Blue", "white.css" => "White", "visual.css" => "Visual");

// or you can directly call value with below line

$alt_stylesheets = array("default.css", "blue.css", "white.css", "visual.css");

$options = array (

array( "name" => $themename." Options",
    "type" => "title"),


array( "name" => "Your section title here",
    "type" => "section"),
array( "type" => "open"),

array("name" => __(\'Colour Scheme\',\'wis\'),
        "desc" => __(\'Select a colour scheme for the theme. Future versions will have multiple styles.\',\'wis\'),
        "id" => "alt_stylesheet",
        "type" => "select",
        "options" => $alt_stylesheets,
        "std" => "white.css"),

array("name" => __(\'Colour Scheme\',\'wis\'),
        "desc" => __(\'Select a colour scheme for the theme. Future versions will have multiple styles.\',\'wis\'),
        "id" => "alt_no",
        "type" => "radio",
        "options" => array("yes" => "Yes", "no" => "Nope"),
        "std" => "no"),

array("name" => __(\'Header Display Ad\',\'wis\'),
        "desc" => __(\'You can show a display ad in header. Paste the code here for 600px by 60px ad.\',\'wis\'),
        "id" => "topbanner",
        "std" => "",
        "type" => "textarea"),

array("name" => __(\'Hide Footer Navigation Links\',\'wis\'),
        "desc" => __(\'Select to hide the navigation bar in the footer. If you want to customize the footer navigation, go to Menus under the Appearance tab in the dashboard.\',\'wis\'),
        "id" => "hide_footer_nav",
        "std" => "",
        "type" => "checkbox"),  

array("name" => __(\'Twitter ID\',\'wis\'),
        "desc" => __(\'Your Twitter user name, please. It will be shown in the navigation bar. Leaving it blank will keep the Twitter icon supressed.\',\'wis\'),
        "id" => "twitterid",
        "type" => "text",
        "std" => ""),       


array( "type" => "close"),

array( "type" => "close")

);


function wis_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: admin.php?page=theme-option.php&saved=true");
die;

} 
else if( \'reset\' == $_REQUEST[\'action\'] ) {

    foreach ($options as $value) {
        delete_option( $value[\'id\'] ); }

    header("Location: admin.php?page=theme-option.php&reset=true");
die;

}
}

add_menu_page($themename, $themename, \'administrator\', basename(__FILE__), \'wis_admin\');
}

function wis_add_init() {

$file_dir=get_bloginfo(\'template_directory\');
wp_enqueue_style("functions", $file_dir."/include/functions/functions.css", false, "1.0", "all");
wp_enqueue_script("rm_script", $file_dir."/include/functions/rm_script.js", false, "1.0");

}
function wis_admin() {

global $themename, $shortname, $options;
$i=0;

if ( $_REQUEST[\'saved\'] ) echo \'<div id="message" class="updated fade"><p><strong>\'.$themename.\' settings saved.</strong></p></div>\';
if ( $_REQUEST[\'reset\'] ) echo \'<div id="message" class="updated fade"><p><strong>\'.$themename.\' settings reset.</strong></p></div>\';

?>
<div class="wrap rm_wrap">
<h2><?php echo $themename; ?> Settings</h2>

<div class="rm_opts">
<form method="post">
<?php foreach ($options as $value) {
switch ( $value[\'type\'] ) {

case "open":
?>

<?php break;

case "close":
?>

</div>
</div>
<br />


<?php break;

case "title":
?>
<p>To easily use the <?php echo $themename;?> theme, you can use the menu below.</p>


<?php break;

case \'text\':
?>

<div class="rm_input rm_text">
    <label for="<?php echo $value[\'id\']; ?>"><?php echo $value[\'name\']; ?></label>
    <input name="<?php echo $value[\'id\']; ?>" id="<?php echo $value[\'id\']; ?>" type="<?php echo $value[\'type\']; ?>" value="<?php if ( get_settings( $value[\'id\'] ) != "") { echo stripslashes(get_settings( $value[\'id\'])  ); } else { echo $value[\'std\']; } ?>" />
 <small><?php echo $value[\'desc\']; ?></small><div class="clearfix"></div>

 </div>
<?php
break;

case \'textarea\':
?>

<div class="rm_input rm_textarea">
    <label for="<?php echo $value[\'id\']; ?>"><?php echo $value[\'name\']; ?></label>
    <textarea name="<?php echo $value[\'id\']; ?>" type="<?php echo $value[\'type\']; ?>" cols="" rows=""><?php if ( get_settings( $value[\'id\'] ) != "") { echo stripslashes(get_settings( $value[\'id\']) ); } else { echo $value[\'std\']; } ?></textarea>
 <small><?php echo $value[\'desc\']; ?></small><div class="clearfix"></div>

 </div>

<?php
break;

case \'select\':
?>

<div class="rm_input rm_select">
    <label for="<?php echo $value[\'id\']; ?>"><?php echo $value[\'name\']; ?></label>

<select name="<?php echo $value[\'id\']; ?>" id="<?php echo $value[\'id\']; ?>">
<?php foreach ($value[\'options\'] as $option) { ?>
        <option <?php if (get_settings( $value[\'id\'] ) == $option) { echo \'selected="selected"\'; } ?>><?php echo $option; ?></option><?php } ?>
</select>

    <small><?php echo $value[\'desc\']; ?></small><div class="clearfix"></div>
</div>
<?php
break;

case "radio":
?>
    <div class="options_input options_select">
        <div class="options_desc"><?php echo $value[\'desc\']; ?></div>
        <span class="labels"><label for="<?php echo $value[\'id\']; ?>"><?php echo $value[\'name\']; ?></label></span>
          <?php foreach ($value[\'options\'] as $key=>$option) {
            $radio_setting = get_option($value[\'id\']);
            if($radio_setting != \'\'){
                if ($key == get_option($value[\'id\']) ) {
                    $checked = "checked=\\"checked\\"";
                    } else {
                        $checked = "";
                    }
            }else{
                if($key == $value[\'std\']){
                    $checked = "checked=\\"checked\\"";
                }else{
                    $checked = "";
                }
            }?>
            <input type="radio" name="<?php echo $value[\'id\']; ?>" value="<?php echo $key; ?>" <?php echo $checked; ?> /><?php echo $option; ?><br />
            <?php } ?>
    </div>

<?php
break;

case "checkbox":
?>

<div class="rm_input rm_checkbox">
    <label for="<?php echo $value[\'id\']; ?>"><?php echo $value[\'name\']; ?></label>

<?php if(get_option($value[\'id\'])){ $checked = "checked=\\"checked\\""; }else{ $checked = "";} ?>
<input type="checkbox" name="<?php echo $value[\'id\']; ?>" id="<?php echo $value[\'id\']; ?>" value="true" <?php echo $checked; ?> />


    <small><?php echo $value[\'desc\']; ?></small><div class="clearfix"></div>
 </div>
<?php break; 
case "section":

$i++;

?>

<div class="rm_section">
<div class="rm_title"><h3><img src="<?php bloginfo(\'template_directory\')?>/include/functions/images/trans.gif" class="inactive" alt="""><?php echo $value[\'name\']; ?></h3><span class="submit"><input name="save<?php echo $i; ?>" type="submit" value="Save changes" />
</span><div class="clearfix"></div></div>
<div class="rm_options">


<?php break;

}
}
?>

<input type="hidden" name="action" value="save" />
</form>
<form method="post">
<p class="submit">
<input name="reset" type="submit" value="Reset" />
<input type="hidden" name="action" value="reset" />
</p>
</form>
 </div> 


<?php
}
?>
<?php
add_action(\'admin_init\', \'wis_add_init\');
add_action(\'admin_menu\', \'wis_add_admin\');
?>
我已经测试并运行良好,只有您可能需要更改您文件的路径(function/function.css等),我还添加了select、radio、textarea、text和checkbox示例。

结束

相关推荐