@Bowe
您需要在选项函数中为复选框创建一个数组,并为其指定一个id作为默认状态,并为其指定“checkbox”类型。下面是一个示例,假设您已经为管理面板准备好了代码
<?php
// Set variables for the options panel
$themename = "your_theme_name";
$themeshortname = "yt";
$mythemeoptions = array();
//The Option function
function cool_theme_options() {
global $themename, $themeshortname, $mythemeoptions;
$themeoptions = array (
array( "name" => __(\'Show featured content slider\',\'your_theme_name\'),
"desc" => __(\'When checked, the slider will be added to the home page.\',\'your_theme_name\'),
"id" => "show_featured_slider",
"std" => "false",
"type" => "checkbox"
),
);
}
//The Option Form
function my_cool_theme_admin() {
global $themename, $themeshortname, $themeoptions;
// Saved or Updated message
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>\';
// The form
?>
<div class="wrap">
<h2><?php echo $themename; ?> Options</h2>
<form method="post">
<?php wp_nonce_field(\'theme-save\'); ?>
<table class="form-table">
<?php foreach ($themeoptions as $value) {
// Output the appropriate form element
switch ( $value[\'type\'] ) {
case \'text\':
?>
<tr valign="top">
<th scope="row"><?php echo $value[\'name\']; ?>:</th>
<td>
<?php foreach ($value[\'options\'] as $key=>$option) {
if ($key == get_option($value[\'id\'], $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 } ?>
<?php echo $value[\'desc\']; ?>
</td>
</tr>
<?php
break;
case "checkbox":
?>
<tr valign="top">
<th scope="row"><?php echo $value[\'name\']; ?></th>
<td>
<?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; ?> />
<?php echo $value[\'desc\']; ?>
</td>
</tr>
<?php
break;
default:
break;
}
}
?>
</table>
<p class="submit">
<input name="save" type="submit" value="Save changes" class="button-primary" />
<input type="hidden" name="action" value="save" />
</p>
</form>
<form method="post">
<?php wp_nonce_field(\'theme-reset\'); ?>
<p class="submit">
<input name="reset" type="submit" value="Reset" />
<input type="hidden" name="action" value="reset" />
</p>
</form>
接下来在函数中添加函数。如果选中该框,则调用滑块的php。
<?php
function cool_theme_slider_option() {
// load the custom options
global $themeoptions;
foreach ($themeoptions as $value) {
$$value[\'id\'] = get_option($value[\'id\'], $value[\'std\']);
}
if ($show_featured_slider == \'true\') {
locate_template( array( \'includes/slider.php\'), true )
}
} // end function
add_action(\'wp_head\', \'cool_theme_slider_option\');
?>
尚未检查此代码以确保其完全按原样工作,但旨在显示在主题中使用复选框选项的示例。