WordPress设置API被构造

时间:2016-12-28 作者:The WP Intermediate

今天整整一天,我几乎没有什么事情让我感到沮丧。我正在学习WordPress设置API的视频课程,该课程由Team Tree House提供。我也在看视频和写代码,但有一点有问题。

我正在复制函数。此处为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>

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

你错过了一些东西。首先,钩住函数wpt_theme_init 具有admin_init 或类似的。否则,您的函数将不会执行。下面的示例

add_action( \'admin_init\', \'wpt_theme_init\' );
add_settings_sectionadd_settings_field 将第四个参数作为$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
}
让我知道它是否工作或需要更多帮助?

相关推荐