API настройки Wordpress поражен


Весь сегодняшний день я был расстроен несколькими вещами. Я изучаю API настройки WordPress из видеокурса, предлагаемого Team Tree House. Я смотрел видео и тоже писал коды, но в какой-то момент у меня возникла проблема.

Я воспроизводю functions.php код файла здесь

function wpt_theme_init() {
    register_setting( 'wptsettings-group', 'wpt_settings' );
    add_settings_section(
        'wpt_slideshow_section',
        'Slideshow Settings',
        'wpt_slideshow_section_callback',
        'general'
    );
    add_settings_field(
        'wpt_slideshow_checkbox',
        'Show slideshow on homepage',
        'wpt_slideshow_checkbox_callback',
        'general',
        'wpt_slideshow_section'
    );
}

// Create Theme Options Page
function wpt_add_theme_page() {

    add_theme_page(
        __('Theme Options', 'wpsettings'),
        __('Theme Options', 'wpsettings'),
        'edit_theme_options',
        'wptsettings',
        'wpt_theme_options_page'
    );

}
add_action('admin_menu', 'wpt_add_theme_page');
function wpt_theme_options_page() {
?>
<div class="wrap">

    <h2>Theme Options - <?php echo get_current_theme(); ?></h2>
    <form method="post" action="options.php">
    <?php
        settings_fields( 'wptsettings-group' );
        do_settings_sections( 'wptsettings' );
        submit_button();
    ?>

</div>

<?php
}


// Enqueue theme styles
function wpt_theme_styles() {

  wp_enqueue_style( 'main_css', get_template_directory_uri() . '/style.css' );

}
add_action( 'wp_enqueue_scripts', 'wpt_theme_styles' );

?>

Я проверял несколько раз, но проблема в том, что я не могу увидеть это на своей странице администратора. Я должен получить этоenter image description here

Но я понимаю это.enter image description here

С утра не мог найти, что шляпы идут не так.

Короче говоря, эта часть не полностью работает →

<div class="wrap">

    <h2>Theme Options - <?php echo get_current_theme(); ?></h2>
    <form method="post" action="options.php">
    <?php
        settings_fields( 'wptsettings-group' );
        do_settings_sections( 'wptsettings' );
    ?>

</div>
Author: The WP Intermediate, 2016-12-28

1 answers

Вы упускаете несколько Вещей. Прежде всего, соедините функцию wpt_theme_init с функцией admin_init или аналогичной. в противном случае ваша функция не будет выполняться. пример ниже

add_action( 'admin_init', 'wpt_theme_init' );

add_settings_section и add_settings_field примите 4-й аргумент в качестве параметра $page. что является wptsettings в вашем случае. помнишь? вы назвали свою страницу wptsettings в add_theme_page (4-й параметр). таким образом, все эти значения должны совпадать.

Неверно

add_settings_section(
    'wpt_slideshow_section',
    'Slideshow Settings',
    'wpt_slideshow_section_callback',
    'general'
);
add_settings_field(
    'wpt_slideshow_checkbox',
    'Show slideshow on homepage',
    'wpt_slideshow_checkbox_callback',
    'general',
    'wpt_slideshow_section'
);

Правильно

add_settings_section(
    'wpt_slideshow_section',
    'Slideshow Settings',
    'wpt_slideshow_section_callback',
    'wptsettings'
);
add_settings_field(
    'wpt_slideshow_checkbox',
    'Show slideshow on homepage',
    'wpt_slideshow_checkbox_callback',
    'wptsettings',
    'wpt_slideshow_section'
);

Наконец, вы не определили функции обратного вызова для секция и поле. это необходимо.

wpt_slideshow_section_callback
wpt_slideshow_checkbox_callback

Функции обратного вызова разделов и полей.

function wpt_slideshow_section_callback() {}
function wpt_slideshow_checkbox_callback() {

$setting = esc_attr( get_option( 'wpt_settings' ) );
?>
<input type="checkbox" name="wpt_settings" value="1"<?php checked( 1 == $setting ); ?> />
<?php

}

Полный правильный код для флажка отображения страницы администратора.

add_action( 'admin_init', 'wpt_theme_init' );
function wpt_theme_init() {
    register_setting( 'wptsettings-group', 'wpt_settings' );
    add_settings_section(
        'wpt_slideshow_section',
        'Slideshow Settings',
        'wpt_slideshow_section_callback',
        'wptsettings'
    );
    add_settings_field(
        'wpt_slideshow_checkbox',
        'Show slideshow on homepage',
        'wpt_slideshow_checkbox_callback',
        'wptsettings',
        'wpt_slideshow_section'
    );
}
function wpt_slideshow_section_callback() {}
function wpt_slideshow_checkbox_callback() {
$setting = esc_attr( get_option( 'wpt_settings' ) );
?>
<input type="checkbox" name="wpt_settings" value="1"<?php checked( 1 == $setting ); ?> />
<?php
}
// Create Theme Options Page
function wpt_add_theme_page() {

    add_theme_page(
        __('Theme Options', 'wpsettings'),
        __('Theme Options', 'wpsettings'),
        'manage_options',
        'wptsettings',
        'wpt_theme_options_page'
    );

}
add_action('admin_menu', 'wpt_add_theme_page');
function wpt_theme_options_page() {
?>
<div class="wrap">

    <h2>Theme Options - <?php echo wp_get_theme(); ?></h2>
    <form method="post" action="options.php">
    <?php
        settings_fields( 'wptsettings-group' );
        do_settings_sections( 'wptsettings' );
        submit_button();
    ?>

</div>

<?php
}

Дайте мне знать, работает ли он или нужна дополнительная помощь?

 1
Author: Anwer AR, 2016-12-28 10:51:05