Выходные значения полей настроек пользовательских тем WP, построенных с использованием класса PHP - PullRequest
0 голосов
/ 27 ноября 2018

Я создал страницу настроек темы с помощью веб-сайта 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 для отображения каждого поля отдельно, как настраиваемое поле метабокса .eg

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

Я пробовал get_option() но не работает.

get_option( string $option, mixed $default = false )
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...