简体   繁体   English

如何在WordPress中添加菜单位置作为管理仪表板菜单

[英]How to add menu locations as admin dashboard menu in WordPress

I have around 5 custom menu locations in wordpress. 我在wordpress中有大约5个自定义菜单位置。 And Now I need to make each of the location as a main side bar navigation in wordpress admin panel. 现在我需要将每个位置作为wordpress管理面板中的主侧栏导航。 I have tried with add_action method. 我尝试过add_action方法。 And this is my code snippet. 这是我的代码片段。

add_action('admin_menu', 'sep_menuexample_create_menu' );
function sep_menuexample_create_menu() {
//create custom top-level menu
add_menu_page( 'My Plugin Settings Page', 'Menu Example Settings','manage_options','navmenu.php', 'sep_menuexample_settings_page',screen_icon('edit'));
}
function sep_menuexample_settings_page(){

}

How can I achieve it? 我怎样才能实现它?

That's only possible with jQuery. 这只有jQuery才有可能。 Create the admin menus and submenus that you want, and add jQuery in admin_head to run in all admin pages. 创建所需的管理菜单和子菜单,并在admin_head添加jQuery以在所有管理页面中运行。

It's a matter of finding your admin menu anchors and changing its href attribute. 这是找到管理菜单锚点并更改其href属性的问题。 In this example, the admin menus are modified to point to nav-menus.php?action=edit&menu=MENU_ID : 在此示例中,管理菜单被修改为指向nav-menus.php?action=edit&menu=MENU_ID

add_action( 'admin_menu', function() {
    add_menu_page( 
        'My custom menu Settings', 
        'Menus', 
        'manage_options', 
        'my-menus', 
        function(){ echo 'This does not show up'; },
        null,
        25
    );

    add_submenu_page( 
        'my-menus' , 
        'My custom submenu-1', 
        'Menu 1', 
        'manage_options', 
        'my-menus', // <---- Same as main menu, change to "sub-menu1" to see effect
        function(){}
    );
    add_submenu_page( 
        'my-menus' , 
        'My custom submenu-2', 
        'Menu 2', 
        'manage_options', 
        'sub-menu2', 
        function(){}
    );
});

# See http://stackoverflow.com/questions/5673269/ for <<<HTML usage
add_action( 'admin_head', function (){
    echo <<<HTML
    <script type="text/javascript">
        jQuery(document).ready( function($) {
            topmenu = $('#toplevel_page_my-menus');
            nav_menu1 = 'nav-menus.php?action=edit&menu=1';
            nav_menu2 = 'nav-menus.php?action=edit&menu=2';
            topmenu.find('a[href="admin.php?page=my-menus"]').attr('href',nav_menu1);  
            topmenu.find('a[href="admin.php?page=sub-menu2"]').attr('href',nav_menu2);
        });     
    </script>
HTML;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM