使用复选框选项添加设置页面的完整代码。您不需要循环和其他东西。WordPress会为你做一切。在中获取更多信息Codex
function wpse_319648_render_popup() {
$options = get_option(\'wpse_319648_checkbox\');
$default = isset($options[\'popup\']) ? $options[\'popup\'] : 0;
printf(
\'<input type="checkbox" name="%1$s[popup]" value="1" %2$s>\',
\'wpse_319648_checkbox\',
checked($default, 1, false)
);
}
function wpse_319648_settings_page() {
echo \'<form class="wrap" action="options.php" method="post">\';
settings_fields(\'wpse-319648-settings\');
do_settings_sections(\'wpse-319648-settings\');
submit_button();
echo \'</form>\';
}
add_action(\'admin_init\', function() {
register_setting(\'wpse-319648-settings\', \'wpse_319648_checkbox\');
add_settings_section(
\'wpse-319648-section\',
__(\'Settings\', \'theme\'),
[],
\'wpse-319648-settings\'
);
add_settings_field(
\'popup\',
__(\'Show popup\', \'theme\'),
\'wpse_319648_render_popup\',
\'wpse-319648-settings\',
\'wpse-319648-section\'
);
});
add_action(\'admin_menu\', function() {
add_submenu_page(\'options-general.php\',
__(\'Options\', \'theme\'),
__(\'Options\', \'theme\'),
\'manage_options\',
\'my-page\',
\'wpse_319648_settings_page\'
);
});
希望有帮助。