为什么WP将我的两个选项页面的内容合并在一起?

时间:2015-03-22 13:44:44

标签: wordpress option

我希望有一个名为&#34的仪表板选项页面; Taux de change"还有一个名为" Telephone"。这段代码应该完成,但由于某种原因将我的两个页面的内容组合在一个页面中(尽管我为两个选项页面使用了不同的函数名称):

add_action('admin_menu', 'add_global_custom_options');
function add_global_custom_options()
{
    add_menu_page('Taux de change', 'Taux de change', 'manage_options', 'functions','global_custom_options','', 83);
}
function global_custom_options()
{
?>
    <div class="wrap">
        <h2>Taux de change</h2>
        <form method="post" action="options.php">
            <?php wp_nonce_field('update-options') ?>
            <p>
                <strong>USD to EU:</strong><br />
                <input type="text" name="usd_to_eu" size="15" value="<?php echo get_option('usd_to_eu'); ?>" />
                <br /><br />
                <strong>USD to CAD:</strong><br />
                <input type="text" name="usd_to_cad" size="15" value="<?php echo get_option('usd_to_cad'); ?>" />
                <br /><br />
                <strong>USD to GBP:</strong><br />
                <input type="text" name="usd_to_gbp" size="15" value="<?php echo get_option('usd_to_gbp'); ?>" />
            </p>
            <p><input type="submit" name="Submit" value="OK" /></p>
            <input type="hidden" name="action" value="update" />
            <input type="hidden" name="page_options" value="usd_to_eu,usd_to_cad,usd_to_gbp" />
        </form>
    </div>
<?php
}

add_action('admin_menu', 'add_global_custom_options2');
function add_global_custom_options2()
{
    add_menu_page('Téléphone', 'Téléphone', 'manage_options', 'functions','global_custom_options2','', 84);
}
function global_custom_options2()
{
?>
    <div class="wrap">
        <h2>Numéro de téléphone</h2>
        <form method="post" action="options.php">
            <?php wp_nonce_field('update-options') ?>
            <p>
                <strong>Numéro de téléphone:</strong><br />
                <input type="text" name="telephone" size="15" value="<?php echo get_option('telephone'); ?>" />
                <br /><br />
            </p>
            <p><input type="submit" name="Submit" value="OK" /></p>
            <input type="hidden" name="action" value="update" />
            <input type="hidden" name="page_options" value="telephone" />
        </form>
    </div>
<?php
}

我做错了什么?

2 个答案:

答案 0 :(得分:0)

add_menu_page()的第四个参数$menu_slug应该是唯一的。您已为两个实例使用functions

https://codex.wordpress.org/Function_Reference/add_menu_page

答案 1 :(得分:0)

您可能需要注册不同的设置组。

add_action('admin_menu', 'add_global_custom_options');
function add_global_custom_options()
{
    add_menu_page('Taux de change', 'Taux de change', 'manage_options', 'functions','global_custom_options','', 83);
    //hook into the custom settings function
    add_action( 'admin_init', 'register_custom_options' );
}
// register the settings needed
function register_custom_options() {
     register_setting( 'custom_options_settings1', 'usd_to_eu' );
     register_setting( 'custom_options_settings1', 'usd_to_cad' );;
     register_setting( 'custom_options_settings1', 'usd_to_cad' );
}
function global_custom_options()
{
?>
    <div class="wrap">
        <h2>Taux de change</h2>
        <form method="post" action="options.php">
            //replace the default nonce with your custom settings fields here
            <?php settings_fields( 'custom_options_settings1' ); ?>
            <p>
                <strong>USD to EU:</strong><br />
                <input type="text" name="usd_to_eu" size="15" value="<?php echo get_option('usd_to_eu'); ?>" />
                <br /><br />
                <strong>USD to CAD:</strong><br />
                <input type="text" name="usd_to_cad" size="15" value="<?php echo get_option('usd_to_cad'); ?>" />
                <br /><br />
                <strong>USD to GBP:</strong><br />
                <input type="text" name="usd_to_gbp" size="15" value="<?php echo get_option('usd_to_gbp'); ?>" />
            </p>
            <p><input type="submit" name="Submit" value="OK" /></p>
            <input type="hidden" name="action" value="update" />
            <input type="hidden" name="page_options" value="usd_to_eu,usd_to_cad,usd_to_gbp" />
        </form>
    </div>
<?php
}

add_action('admin_menu', 'add_global_custom_options2');
function add_global_custom_options2()
{
    add_menu_page('Téléphone', 'Téléphone', 'manage_options', 'functions','global_custom_options2','', 84);
    add_action( 'admin_init', 'register_custom_options2' );
}
// register the settings needed
function register_custom_options2() {
     register_setting( 'custom_options_settings2', 'telephone' );
}
function global_custom_options2()
{
?>
    <div class="wrap">
        <h2>Numéro de téléphone</h2>
        <form method="post" action="options.php">
            <?php settings_fields( 'custom_options_settings2' ); ?>
            <p>
                <strong>Numéro de téléphone:</strong><br />
                <input type="text" name="telephone" size="15" value="<?php echo get_option('telephone'); ?>" />
                <br /><br />
            </p>
            <p><input type="submit" name="Submit" value="OK" /></p>
            <input type="hidden" name="action" value="update" />
            <input type="hidden" name="page_options" value="telephone" />
        </form>
    </div>
<?php
}

有关详细信息,请参阅WP Codex:http://codex.wordpress.org/Creating_Options_Pages

基本上你需要注册不同的选项集以将它们分成多个页面,或者据我所知它将它们默认为一页。希望这适合你。