Wordpress,管理员自定义菜单标题在子菜单上重复

时间:2014-10-04 07:09:41

标签: wordpress

我是wordpress的新手,我正在尝试在后端创建自定义菜单。 我按照这个http://codex.wordpress.org/Administration_Menus链接创建了菜单,但是我有一个小问题。

Test TopLevel
    Test TopLevel
    Test Top Sub Level
    Test Top Sub Level 2

我想删除子菜单下的'Test TopLevel',我想要的最终结构如下

Test TopLevel    
    Test Top Sub Level
    Test Top Sub Level 2

感谢您的所有时间。


菜单代码     http://codex.wordpress.org/Adding_Administration_Menus     说明:菜单测试     作者:Codex作者     作者URI:http://example.com     * /

// Hook for adding admin menus
add_action('admin_menu', 'mt_add_pages');

// action function for above hook
function mt_add_pages() {
    // Add a new submenu under Settings:
    add_options_page(__('Test Settings','menu-test'), __('Test Settings','menu-test'), 'manage_options', 'testsettings', 'mt_settings_page');

    // Add a new submenu under Tools:
    add_management_page( __('Test Tools','menu-test'), __('Test Tools','menu-test'), 'manage_options', 'testtools', 'mt_tools_page');

    // Add a new top-level menu (ill-advised):
    add_menu_page(__('Test Toplevel','menu-test'), __('Test Toplevel','menu-test'), 'manage_options', 'mt-top-level-handle', 'mt_toplevel_page' );

    // Add a submenu to the custom top-level menu:
    add_submenu_page('mt-top-level-handle', __('Test Sublevel','menu-test'), __('Test Sublevel','menu-test'), 'manage_options', 'sub-page', 'mt_sublevel_page');

    // Add a second submenu to the custom top-level menu:
    add_submenu_page('mt-top-level-handle', __('Test Sublevel 2','menu-test'), __('Test Sublevel 2','menu-test'), 'manage_options', 'sub-page2', 'mt_sublevel_page2');
}

// mt_settings_page() displays the page content for the Test settings submenu
function mt_settings_page() {
    echo "<h2>" . __( 'Test Settings', 'menu-test' ) . "</h2>";
}

// mt_tools_page() displays the page content for the Test Tools submenu
function mt_tools_page() {
    echo "<h2>" . __( 'Test Tools', 'menu-test' ) . "</h2>";
}

// mt_toplevel_page() displays the page content for the custom Test Toplevel menu
function mt_toplevel_page() {
    echo "<h2>" . __( 'Test Toplevel', 'menu-test' ) . "</h2>";
}

// mt_sublevel_page() displays the page content for the first submenu
// of the custom Test Toplevel menu
function mt_sublevel_page() {
    echo "<h2>" . __( 'Test Sublevel', 'menu-test' ) . "</h2>";
}

// mt_sublevel_page2() displays the page content for the second submenu
// of the custom Test Toplevel menu
function mt_sublevel_page2() {
    echo "<h2>" . __( 'Test Sublevel2', 'menu-test' ) . "</h2>";
}

?>

1 个答案:

答案 0 :(得分:1)

由于您不熟悉 wordpress ,您可能不会注意到这一点。但是,让我告诉您,通常您添加的菜单会成为 1st子菜单即可。您可以通过安装插件来注意它。

但您也可以通过以下代码自定义它:

    // Hook for adding admin menus
    add_action('admin_menu', 'mt_add_pages');

    // action function for above hook
    function mt_add_pages() {
        // Add a new submenu under Settings:
        add_options_page(__('Test Settings','menu-test'), __('Test Settings','menu-test'), 'manage_options', 'testsettings', 'mt_settings_page');

        // Add a new submenu under Tools:
        add_management_page( __('Test Tools','menu-test'), __('Test Tools','menu-test'), 'manage_options', 'testtools', 'mt_tools_page');

        // Add a new top-level menu (ill-advised):
        add_menu_page(__('Test Toplevel','menu-test'), __('Test botlevel','menu-test'), 'manage_options', 'mt-top-level-handle', null );

        // Add a submenu to the custom top-level menu:
        add_submenu_page('mt-top-level-handle', __('Test Sublevel','menu-test'), __('Test Sublevel','menu-test'), 'manage_options', 'mt-top-level-handle', 'mt_sublevel_page');

        // Add a second submenu to the custom top-level menu:
        add_submenu_page('mt-top-level-handle', __('Test Sublevel 2','menu-test'), __('Test Sublevel 2','menu-test'), 'manage_options', 'sub', 'mt_sublevel_page2');
    }

    // mt_settings_page() displays the page content for the Test settings submenu
    function mt_settings_page() {
        echo "<h2>" . __( 'Test Settings', 'menu-test' ) . "</h2>";
    }

    // mt_tools_page() displays the page content for the Test Tools submenu
    function mt_tools_page() {
        echo "<h2>" . __( 'Test Tools', 'menu-test' ) . "</h2>";
    }

    // mt_sublevel_page() displays the page content for the first submenu
    // of the custom Test Toplevel menu
    function mt_sublevel_page() {
        echo "<h2>" . __( 'Test Sublevel', 'menu-test' ) . "</h2>";
    }

    // mt_sublevel_page2() displays the page content for the second submenu
    // of the custom Test Toplevel menu
    function mt_sublevel_page2() {
        echo "<h2>" . __( 'Test Sublevel2', 'menu-test' ) . "</h2>";
    }

我在这里做的是,我将null提供给add_menu_page,除此之外,我已将 SAME SLUG 提供给 SUB MENU < / strong> MENU 了。 在我们的例子中,它是mt-top-level-handle

注意:有了这个,您就无法执行以下操作:

测试TOPLEVEL
    测试TOP子级别

您需要有多个子菜单。