使用PHP类构建的自定义WP主题设置字段的输出值

时间:2018-11-27 18:18:37

标签: php wordpress wordpress-theming

我已经使用wp-hasty网站生成了主题设置页面。我试图在不同的主题模板中显示每个字段内容。这是主题选项的代码

/* Advertising Settings Page */
class advertising_Settings_Page {
    public function __construct() {
        add_action( 'admin_menu', array( $this, 'wph_create_settings' ) );
        add_action( 'admin_init', array( $this, 'wph_setup_sections' ) );
        add_action( 'admin_init', array( $this, 'wph_setup_fields' ) );
    }
    public function wph_create_settings() {
        $page_title = 'Advertising';
        $menu_title = 'Advertising';
        $capability = 'manage_options';
        $slug = 'advertising';
        $callback = array($this, 'wph_settings_content');
        add_theme_page($page_title, $menu_title, $capability, $slug, $callback);
    }
    public function wph_settings_content() { ?>
        <div class="wrap">
            <h1>Advertising</h1>
            <?php settings_errors(); ?>
            <form method="POST" action="options.php">
                <?php
                    settings_fields( 'advertising' );
                    do_settings_sections( 'advertising' );
                    submit_button();
                ?>
            </form>
        </div> <?php
    }
    public function wph_setup_sections() {
        add_settings_section( 'advertising_section', '', array(), 'advertising' );
    }
    public function wph_setup_fields() {
        $fields = array(
            array(
                'label' => 'before_game',
                'id' => 'beforegame_48156',
                'type' => 'textarea',
                'section' => 'advertising_section',
            ),
            array(
                'label' => 'after_game',
                'id' => 'aftergame_14160',
                'type' => 'text',
                'section' => 'advertising_section',
            ),
            array(
                'label' => 'game_right',
                'id' => 'gameright_86515',
                'type' => 'text',
                'section' => 'advertising_section',
            ),
            array(
                'label' => 'before_game_loop',
                'id' => 'beforegameloo_63696',
                'type' => 'text',
                'section' => 'advertising_section',
            ),
        );
        foreach( $fields as $field ){
            add_settings_field( $field['id'], $field['label'], array( $this, 'wph_field_callback' ), 'advertising', $field['section'], $field );
            register_setting( 'advertising', $field['id'] );
        }
    }
    public function wph_field_callback( $field ) {
        $value = get_option( $field['id'] );
        switch ( $field['type'] ) {
                case 'textarea':
                printf( '<textarea name="%1$s" id="%1$s" placeholder="%2$s" rows="5" cols="50">%3$s</textarea>',
                    $field['id'],
                    $field['placeholder'],
                    $value
                    );
                    break;
            default:
                printf( '<input name="%1$s" id="%1$s" type="%2$s" placeholder="%3$s" value="%4$s" />',
                    $field['id'],
                    $field['type'],
                    $field['placeholder'],
                    $value
                );
        }
        if( $desc = $field['desc'] ) {
            printf( '<p class="description">%s </p>', $desc );
        }
    }
}
new advertising_Settings_Page();

能否请我指导Wordpress函数以像metabox自定义字段一样分别显示每个字段。例如

get_post_meta( int $post_id, string $key = '', bool $single = false )

我也尝试过get_option(),但是没有用。

get_option( string $option, mixed $default = false )

0 个答案:

没有答案