你错过了一些东西。首先,钩住函数wpt_theme_init
具有admin_init
或类似的。否则,您的函数将不会执行。下面的示例
add_action( \'admin_init\', \'wpt_theme_init\' );
add_settings_section
和
add_settings_field
将第四个参数作为
$page
参数这是
wptsettings
在你的情况下。回想起您已命名页面
wptsettings
在里面
add_theme_page
(第4个参数)。因此,所有这些值都必须匹配。
不准确的
add_settings_section(
\'wpt_slideshow_section\',
\'Slideshow Settings\',
\'wpt_slideshow_section_callback\',
\'general\'
);
add_settings_field(
\'wpt_slideshow_checkbox\',
\'Show slideshow on homepage\',
\'wpt_slideshow_checkbox_callback\',
\'general\',
\'wpt_slideshow_section\'
);
正确的
add_settings_section(
\'wpt_slideshow_section\',
\'Slideshow Settings\',
\'wpt_slideshow_section_callback\',
\'wptsettings\'
);
add_settings_field(
\'wpt_slideshow_checkbox\',
\'Show slideshow on homepage\',
\'wpt_slideshow_checkbox_callback\',
\'wptsettings\',
\'wpt_slideshow_section\'
);
最后,您还没有为节和字段定义回调函数。这是必需的。
wpt_slideshow_section_callback
wpt_slideshow_checkbox_callback
节和字段回调函数。
function wpt_slideshow_section_callback() {}
function wpt_slideshow_checkbox_callback() {
$setting = esc_attr( get_option( \'wpt_settings\' ) );
?>
<input type="checkbox" name="wpt_settings" value="1"<?php checked( 1 == $setting ); ?> />
<?php
}
“管理页面显示”复选框的完整正确代码。
add_action( \'admin_init\', \'wpt_theme_init\' );
function wpt_theme_init() {
register_setting( \'wptsettings-group\', \'wpt_settings\' );
add_settings_section(
\'wpt_slideshow_section\',
\'Slideshow Settings\',
\'wpt_slideshow_section_callback\',
\'wptsettings\'
);
add_settings_field(
\'wpt_slideshow_checkbox\',
\'Show slideshow on homepage\',
\'wpt_slideshow_checkbox_callback\',
\'wptsettings\',
\'wpt_slideshow_section\'
);
}
function wpt_slideshow_section_callback() {}
function wpt_slideshow_checkbox_callback() {
$setting = esc_attr( get_option( \'wpt_settings\' ) );
?>
<input type="checkbox" name="wpt_settings" value="1"<?php checked( 1 == $setting ); ?> />
<?php
}
// Create Theme Options Page
function wpt_add_theme_page() {
add_theme_page(
__(\'Theme Options\', \'wpsettings\'),
__(\'Theme Options\', \'wpsettings\'),
\'manage_options\',
\'wptsettings\',
\'wpt_theme_options_page\'
);
}
add_action(\'admin_menu\', \'wpt_add_theme_page\');
function wpt_theme_options_page() {
?>
<div class="wrap">
<h2>Theme Options - <?php echo wp_get_theme(); ?></h2>
<form method="post" action="options.php">
<?php
settings_fields( \'wptsettings-group\' );
do_settings_sections( \'wptsettings\' );
submit_button();
?>
</div>
<?php
}
让我知道它是否工作或需要更多帮助?