我正在WordPress中为插件创建管理菜单。我已使用add\\u menu\\u page()成功注册了菜单,并按预期显示在侧栏中。我还使用add\\u submenu\\u page()在菜单中添加了几个部分。
WordPress(运行3.05)似乎没有正确指示应将哪个菜单项激活为“当前”。
当我将测试页面添加到主仪表板菜单时,我看到的是:
http://dl.dropbox.com/u/3019972/wp-screen1.png
正如您所看到的,当前页面在菜单中视觉上是正确的,并且被赋予了一个“current”类。然而,在我自己的菜单上,它看起来是这样的:
http://dl.dropbox.com/u/3019972/wp-screen2.png
在本例中,我希望突出显示“Dashboard”。
这只是WordPress的一个bug吗?或者我需要提供特殊代码才能在我自己的菜单中工作?
Updated with code examples
我用于添加和创建菜单的代码如下所示:
add_action(\'admin_menu\',\'my_admin_menu\');
function my_admin_menu() {
// this test works as expected
// add_submenu_page( \'index.php\', \'test\', \'test\', \'read\', \'admin.php?page=plugin-dashboard\',\'my_pluggin_dashboard\' );
// my main menu menu
add_menu_page(__(\'My Plugin\', \'myplugin\'),__(\'My Plugin\', \'myplugin\'), \'edit_posts\',\'admin.php?page=plugin-dashboard\',\'my_plugin_dashboard\',\'icon\');
// dashboard submenu - this fails to highlight with current
add_submenu_page(\'admin.php?page=plugin-dashboard\', __(\'Dashboard\',\'myplugin\'), __(\'Dashboard\',\'myplugin\'), \'edit_posts\', \'admin.php?page=plugin-dashboard\', \'my_plugin_dashboard\' );
// settings submenu - this fails to highlight with current
add_submenu_page(\'admin.php?page=plugin-dashboard\', __(\'Settings\',\'myplugin\'), __(\'Settings\',\'myplugin\'), \'manage_options\', \'admin.php?page=my-plugin-settings\', \'my_plugin_settings\' );
}
我已经对工作正常的代码和行为不符合预期的代码进行了注释。感谢您的评论。
最合适的回答,由SO网友:wyrfel 整理而成
试试这个:
add_menu_page(__(\'My Plugin\', \'myplugin\'),__(\'My Plugin\', \'myplugin\'), \'edit_posts\',\'my-plugin-dashboard\',\'my_plugin_dashboard\',\'icon\');
// dashboard submenu - this fails to highlight with current
add_submenu_page(\'my-plugin-dashboard\', __(\'Dashboard\',\'myplugin\'), __(\'Dashboard\',\'myplugin\'), \'edit_posts\', \'my-plugin-dashboard\', \'my_plugin_dashboard\' );
// settings submenu - this fails to highlight with current
add_submenu_page(\'my-plugin-dashboard\', __(\'Settings\',\'myplugin\'), __(\'Settings\',\'myplugin\'), \'manage_options\', \'my-plugin-settings\', \'my_plugin_settings\' );
本质上:不要与管理员使用完整的页面链接。php?。。。作为页面slug。尤其是在传递回调时,只需使用slug,而不是页面地址。