WordPress API设置:呈现为不显示的部分的标题

时间:2017-06-05 作者:The WP Intermediate

请参阅下面提到的代码→

    function klogeto_initialize_theme_options() {
    // Let\'s introduce a section for the header options
    add_settings_section(
        \'header_section\',                           // The ID to use for this section in attribute tags
        \'Header Options\',                           // The title of the section rendered to the screen
        \'klogeto_logo_display\',                     // The function used to render the options for this section
        \'klogeto-theme-header-options\'              // The ID of the page on which this section is rendered
    );

    add_settings_field(
    \'display_logo\',                              // The ID (or the name) of the field
    \'Upload a logo\',                             // The text used to label the field
    \'klogeto_logo_display_field\',                // The callback function used to render the field
    \'klogeto-theme-header-logo-options\',         // The page on which we\'ll be rendering this field
    \'header_section\'                             // The section to which we\'re adding the setting
    );
}

add_action( \'admin_init\', \'klogeto_initialize_theme_options\' );
但问题是文本Header Options 未显示在“主题选项”面板中。

\'标题选项\',//呈现到屏幕的节的标题

我犯了什么错误?看起来像是我无法诊断的愚蠢错误。

我忘了提到我也写了这行代码→

    register_setting(
    \'header_section\',                   // The name of the group of settings
    \'header_options\',                   // The name of the actual option (or setting)
    \'klogeto_sanitize_header_options\'       // The callback to footer sanitization option
);

1 个回复
最合适的回答,由SO网友:Johansson 整理而成

您提供的代码缺少很多节,当我在本地主机上使用它时,它不会输出任何内容。我已经为您编写并测试了一个完整的类,可以满足您的需要。

// Start Class
if ( ! class_exists( \'my_Theme_Options\' ) ) {
    class my_Theme_Options {
        public function __construct() {
            // We only need to register the admin panel on the back-end
            if ( is_admin() ) {
                add_action( \'admin_menu\', array( \'my_Theme_Options\', \'my_add_admin_menu\' ) );
                add_action( \'admin_init\', array( \'my_Theme_Options\', \'my_register_settings\' ) );
            }
        }
        // Returns all theme options 
        public static function get_my_theme_options() {
            return get_option( \'my_theme_options\' );
        }
        // Returns single theme option
        public static function my_get_theme_option_value( $id ) {
            $options = self::get_my_theme_options();
            if ( isset( $options[$id] ) ) {
                return $options[$id];
            }
        }
        // Add sub menu page
        public static function my_add_admin_menu() {
            add_options_page(
                esc_html__( \'my Options\', \'text-domain\' ),
                esc_html__( \'my Options\', \'text-domain\' ),
                \'manage_options\',
                \'my-theme-settings\',
                array( \'my_Theme_Options\', \'my_create_admin_page\' )
            );
        }
        // Register a setting and its sanitization callback.
        public static function my_register_settings() {
            register_setting( \'my_theme_options\', \'my_theme_options\', array( \'my_Theme_Options\', \'my_sanitize\' ) );
        }
        // Sanitization callback
        public static function my_sanitize( $options ) {
            // If we have options lets sanitize them
            if ( $options ) {
                // Input
                if ( ! empty( $options[\'sample_input\'] ) ) {
                    $options[\'sample_input\'] = sanitize_text_field( $options[\'sample_input\'] );
                } else {
                    unset( $options[\'sample_input\'] ); // Remove from options if empty
                }
            }
            // Return sanitized options
            return $options;
        }
        // Now we output our form to save and view our options
        public static function my_create_admin_page() { ?>
            <div class="wrap">
                <h1><?php esc_html_e( \'Theme Options\', \'text-domain\' ); // The heading for Option page ?></h1>
                <?php settings_errors(); // Output any error if exists ?>
                <form method="post" action="options.php">
                    <?php settings_fields( \'my_theme_options\' ); ?>
                    <?php esc_html_e( \'Heading\', \'text-domain\' ); ?>
                    <table class="form-table">
                        <tbody>
                            <tr>
                                <th scope="row">
                                    <?php esc_html_e( \'Sample Input\', \'text-domain\' ); ?>
                                </th>
                                <td>
                                    <?php $value = self::my_get_theme_option_value( \'sample_input\' ); // We retrieve and output this option in the input area ?>
                                    <input type="text" name="my_theme_options[sample_input]" value="<?php echo esc_attr( $value ); ?>">
                                </td>
                            </tr>
                        </tbody>
                    </table>
                    <?php submit_button(); // Let\'s have a button to save the form, shall we? ?>
                </form>
            </div>
        <?php }
    }
}
new my_Theme_Options();
// Helper function to use in theme to return a theme option value
function my_get_theme_option( $id = \'\' ) {
    return my_Theme_Options::my_get_theme_option_value( $id );
}
我使用了一个简单的文本输入作为示例,但您可以使用复选框、单选按钮或任何您想要的东西。

我已经尽我所能添加了解释,但是如果您对代码的一部分有任何疑问,请确保让我知道。

结束

相关推荐