标准是选项的默认值。我只需要选择字段的默认值。我尝试过很多东西,但似乎没什么用。我真的需要帮助。我不知道从哪里开始。
所以在这里,我的功能是将设置添加到我的选项页面。
function nl_register_function_name() {
// Validation callback
register_setting( 'nl_theme_options', 'nl_theme_options', 'nl_validate_settings' );
// Add setting to section
add_settings_section( 'nl_styling_section', 'Styling', 'nl_display_styling_section', 'nl_theme_options.php' );
// Create selection field
$field_args = array(
'type' => 'select',
'id' => 'nl_function_name',
'name' => 'nl_function_name',
'desc' => 'Select border bottom size.',
'std' => 'Small',
'label_for' => 'nl_function_name',
'items' => array(
"None",
"Small",
"Medium",
"Large"
)
);
// Label
add_settings_field( 'label_function_name', 'function_name', 'nl_display_setting', 'nl_theme_options.php', 'nl_styling_section', $field_args );
}
// Registers the setting
add_action( 'admin_init', 'nl_register_function_name' );
在这里,我为选择字段创建HTML。
function nl_display_setting ( $args ) {
extract( $args );
$option_name = 'nl_theme_options';
$options = get_option( $option_name );
switch ( $args['type'] ) {
case 'select':
if( isset( $options[$id] ) ) {
$options[$id] = stripslashes( $options[$id] );
$options[$id] = esc_attr( $options[$id] );
echo "<select name='" . $option_name . "[$id]' value='$options[$id]'>";
foreach( $items as $item ) {
$selected = ($options[$id]==$item) ? 'selected="selected"' : '';
echo "<option value='$item' $selected>$item</option>";
}
} elseif ( !isset( $options[$id] ) ) {
$std = $args['std'];
echo "<select name='" . $option_name . "[$id]' value='$std'>";
foreach( $items as $item ) {
echo "<option value='$item'>$item</option>";
}
}
echo "</select>";
echo ($desc != '') ? "<br><span class='description'>$desc</span>" : "";
break;
}
}
如何使用WordPress API使默认设置适用于选择字段?
答案 0 :(得分:2)
我修好了!我将在这里发布解决方案,以便将来可能遇到同样的问题,因为似乎没有任何在线解决方案。
case 'select':
if( isset( $options[$id] ) ) {
$options[$id] = stripslashes( $options[$id] );
$options[$id] = esc_attr( $options[$id] );
echo "<select name='" . $option_name . "[$id]' value='$options[$id]'>";
foreach( $items as $item ) {
$selected = ($options[$id]==$item) ? 'selected="selected"' : '';
echo "<option value='$item' $selected>$item</option>";
}
} elseif ( !isset( $options[$id] ) ) {
echo "<select name='" . $option_name . "[$id]' value='$std'>";
foreach( $items as $item ) {
$selected = ($std==$item) ? 'selected="selected"' : '';
echo "<option value='$item' $selected>$item</option>";
}
}
echo "</select>";
echo ($desc != '') ? "<br><span class='description'>$desc</span>" : "";
break;