0

我是 PHP 新手,我正在尝试在 wordpress 中创建主题选项。其余的主题选项工作正常,但我创建的选项 3 没有在前端显示文本

<?php
//register settings
function theme_settings_init(){
    register_setting( 'theme_settings', 'theme_settings' );
}

//add settings page to menu
function add_settings_page() {
add_menu_page( __( 'Theme Settings' ), __( 'Theme Settings' ), 'manage_options', 'settings', 'theme_settings_page');
}

//add actions
add_action( 'admin_init', 'theme_settings_init' );
add_action( 'admin_menu', 'add_settings_page' );

//define your variables
$color_scheme = array('default','blue','green',);

//start settings page
function theme_settings_page() {

if ( ! isset( $_REQUEST['updated'] ) )
$_REQUEST['updated'] = false;

//get variables outside scope
global $color_scheme;
?>

<div>

<div id="icon-options-general"></div>
<h2><?php _e( 'Elegant Theme Settings' ) //your admin panel title ?></h2>

<?php
//show saved options message
if ( false !== $_REQUEST['updated'] ) : ?>
<div><p><strong><?php _e( 'Options saved' ); ?></strong></p></div>
<?php endif; ?>

<form method="post" action="options.php">

<?php settings_fields( 'theme_settings' ); ?>
<?php $options = get_option( 'theme_settings' ); ?>

<table>

<!-- Option 1: Custom Logo -->
<tr valign="top">
<th scope="row"><?php _e( 'Custom Logo' ); ?></th>
<td><input id="theme_settings[custom_logo]" type="text" size="36" name="theme_settings[custom_logo]" value="<?php esc_attr_e( $options['custom_logo'] ); ?>" />
<label for="theme_settings[custom_logo]"><?php _e( 'Enter the URL to your custom logo' ); ?></label></td>
</tr>

<!-- Option 2: Color Scheme -->
<tr valign="top">
<th scope="row"><?php _e( 'Color Scheme' ); ?></th>
<td><select name="theme_settings[color_scheme]">
<?php foreach ($color_scheme as $option) { ?>
<option <?php if ($options['color_scheme'] == $option ){ echo 'selected="selected"'; } ?>><?php echo htmlentities($option); ?></option>
<?php } ?>
</select>                    
<label for="theme_settings[color_scheme]"><?php _e( 'Choose Your Color Scheme' ); ?></label></td>
</tr>

<!-- Option 3: Intro Code -->
<tr valign="top">
<th scope="row"><?php _e( 'Intro' ); ?></th>
<td><label for="theme_settings[headline]"><?php _e( 'Enter your Intro Headline' ); ?></label>
<br />
<textarea id="theme_settings[headline]" name="theme_settings[headline]" rows="5" cols="36"><?php esc_attr_e( $options['headline'] ); ?></textarea></td>
</tr>


<!-- Option 4: Tracking Code -->
<tr valign="top">
<th scope="row"><?php _e( 'Tracking Code' ); ?></th>
<td><label for="theme_settings[tracking]"><?php _e( 'Enter your analytics tracking code' ); ?></label>
<br />
<textarea id="theme_settings[tracking]" name="theme_settings[tracking]" rows="5" cols="36"><?php esc_attr_e( $options['tracking'] ); ?></textarea></td>
</tr>

</table>

<p><input name="submit" id="submit" value="Save Changes" type="submit"></p>
</form>

</div><!-- END wrap -->

<?php
}
//sanitize and validate
function options_validate( $input ) {
    global $select_options, $radio_options;
    if ( ! isset( $input['option'] ) )
        $input['option'] = null;
    $input['option'] = ( $input['option'] == 1 ? 1 : 0 );
    $input['sometext'] = wp_filter_nohtml_kses( $input['sometext'] );
    if ( ! isset( $input['radioinput'] ) )
        $input['radioinput'] = null;
    if ( ! array_key_exists( $input['radioinput'], $radio_options ) )
        $input['radioinput'] = null;
    $input['sometextarea'] = wp_filter_post_kses( $input['sometextarea'] );
    return $input;
}
?>

所有其他选项都工作正常,但是当我在介绍中添加文本框时,它没有在前端显示内容

<?php //show tracking code for the header 
    echo $options['headline'];?>

请帮我

4

1 回答 1

0

您在第二个选项中直接使用 $options 。

<option <?php if ($options['color_scheme'] == $option ){ echo 'selected="selected"'; } ?>><?php echo htmlentities($option); ?></option>

我认为它正在覆盖您的第二个选项。

于 2012-04-19T06:13:31.473 回答