简体   繁体   English

如何将子菜单添加到WordPress管理栏

[英]How can I add a Submenu to the WordPress Admin Bar

I need to add a dropdown menu to Wordpress in the admin bar to include multiple links. 我需要在管理栏中向Wordpress添加一个下拉菜单,以包含多个链接。 What is the best solution? 最好的解决方案是什么?

I was looking for the answer to this question for a while and couldn't find the solution on here so I thought this would help! 我一直在寻找这个问题的答案,但在这里找不到解决方案,因此我认为这会有所帮助! I found a great blog post and the perfect solution to my question: 我找到了一篇很棒的博客文章,也为我的问题提供了完美的解决方案:

http://davidwalsh.name/add-submenu-wordpress-admin-bar http://davidwalsh.name/add-submenu-wordpress-admin-bar

Like adding functionality to your theme and other admin area, the directives will go in your theme's functions.php file. 就像在主题和其他管理区域中添加功能一样,这些指令也将放入主题的functions.php文件中。 The code itself should be self explanatory: 代码本身应该是自解释的:

function create_dwb_menu() {
    global $wp_admin_bar;

    $menu_id = 'dwb';
    $wp_admin_bar->add_menu(array('id' => $menu_id, 'title' => __('DWB'), 'href' => '/'));
    $wp_admin_bar->add_menu(array('parent' => $menu_id, 'title' => __('Homepage'), 'id' => 'dwb-home', 'href' => '/', 'meta' => array('target' => '_blank')));
    $wp_admin_bar->add_menu(array('parent' => $menu_id, 'title' => __('Drafts'), 'id' => 'dwb-drafts', 'href' => 'edit.php?post_status=draft&post_type=post'));
    $wp_admin_bar->add_menu(array('parent' => $menu_id, 'title' => __('Pending Comments'), 'id' => 'dwb-pending', 'href' => 'edit-comments.php?comment_status=moderated'));
}
add_action('admin_bar_menu', 'create_dwb_menu', 2000);

Setting an id on the parent menu item allows you to use the parent key for submenu items; 在父菜单项上设置一个ID可使您将父键用于子菜单项。 the rest of the keys are easy to figure out. 其余的键很容易弄清楚。 With the menu created, you simply need to add the WordPress hook and specificity to add it! 创建菜单后,您只需要添加WordPress钩子和特殊性即可添加它!

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

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