这是一个快速选项页面,您可以使用get_pages
作用这个Settings API 负责为您保存选项。然后您可以使用get_option
在模板中加载选项数组,以及get_post
加载与保存在选项中的ID关联的post数据。
add_action( \'admin_init\', \'russ_options_init\' );
add_action( \'admin_menu\', \'russ_options_page\' );
function russ_options_init(){
register_setting(
\'russ_options_group\',
\'russ_options\',
\'russ_options_validate\'
);
}
function russ_options_page() {
add_options_page(
\'Russ Options\',
\'Russ Options\',
\'manage_options\',
\'russ_options\',
\'russ_render_options\'
);
}
function russ_render_options() {
?>
<div class="wrap">
<form method="post" action="options.php">
<?php
settings_fields( \'russ_options_group\' );
$options = get_option( \'russ_options\' );
?>
<table class="form-table">
<tr valign="top"><th scope="row">Choose a page</th>
<td>
<select name="russ_options[page_id]">
<?php
if( $pages = get_pages() ){
foreach( $pages as $page ){
echo \'<option value="\' . $page->ID . \'" \' . selected( $page->ID, $options[\'page_id\'] ) . \'>\' . $page->post_title . \'</option>\';
}
}
?>
</select>
</td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e(\'Save Changes\') ?>" />
</p>
</form>
</div>
<?php
}
function russ_options_validate( $input ) {
// do some validation here if necessary
return $input;
}