更改管理菜单部分的顺序?
5 个回答
- 投票数
-
- 2010-09-02
您好 @BinaryBit:
难怪你有点沮丧;通过WordPress核心,管理菜单是最令人费解和令人沮丧的实现之一.老实说,我不知道他们以这种方式设计时的想法.
@EAMann 做了一项 优秀 的工作,解释了WordPress管理员菜单的工作方式(我希望我已经能够阅读有关4个月前... :)
仍然,在我弄清楚它是如何工作的之后,我仍然茫然不知所措,而又没有花足够的时间来保持直立,而我试图做一些简单的事情. 这就是我构建菜单API的原因,该API简化并简化了WordPress管理菜单的使用.
它们与WordPress的现有结构100%兼容,并且由于我一直是唯一使用它的人,所以它们仍然具有很大的alpha版本.我确定有些用例尚未处理.但我会将代码发布在这里,供您和其他人试用.
您可以下载文件,将其放入主题目录中: wp-admin -menu-classes.php ,以下内容显示了如何调用主题的
functions.php
文件中的函数:<?php require_once('wp-admin-menu-classes.php'); add_action('admin_menu','my_admin_menu'); function my_admin_menu() { swap_admin_menu_sections('Pages','Posts'); // Swap location of Posts Section with Pages Section rename_admin_menu_section('Media','Photos & Video'); // Rename Media Section to "Photos & Video" delete_admin_menu_section('Links'); // Get rid of Links Section $movie_tags_item_array = get_admin_menu_item_array('Movies','Movie Tags'); // Save off the Movie Tags Menu update_admin_menu_section('Movies',array( // Rename two Movie Menu Items and Delete the Movie Tags Item array('rename-item','item'=>'Movies','new_title'=>'List Movies'), array('rename-item','item'=>'Add New','new_title'=>'Add Movie'), array('delete-item','item'=>'Movie Tags'), )); copy_admin_menu_item('Movies',array('Actors','Add New')); // Copy the 'Add New' over from Actors renamed_admin_menu_item('Movies','Add New','Add Actor'); // Rename copied Actor 'Add New' to 'Add Actor add_admin_menu_item('Movies',array( // (Another way to get a 'Add Actor' Link to a section.) 'title' => 'Alt Add Actor ', 'slug' => 'post-new.php?post_type=actor', ), array(// Add Back the Movie Tags at the end. 'where'=>'end' )); add_admin_menu_item('Movies',$movie_tags_item_array,array(// Add Back the Movie Tags at the end. 'where'=>'end' )); delete_admin_menu_section('Actors'); // Finally just get rid of the actors section }
此外,这些功能甚至 正在考虑(作为基础)包含在WordPress 3.1中 > ,所以如果幸运的话,这些甚至可能成为标准!
Hi @BinaryBit:
It's no wonder you are a bit frustrated; the admin menu is one of the most obtuse and frustrating implementations through WordPress core. Honestly, I don't know what they were thinking when they designed it that way.
@EAMann did an excellent job of explaining how the admin menus work in WordPress (I wish I had been able to read that about 4 months ago... :)
Still, after I figured it out how it worked I was still at a loss to work with it without devoting enough time to keep my head straight while I tried to do simple things. So that's why I built a Menu API that simplifies and streamlines working with the WordPress admin menu.
They are 100% compatible with WordPress' existing structures and still very much in alpha since I've been the only one using it. I'm sure there are use-cases they do not yet handle. But I'll post the code here for you and others to try out.
You can download the file to drop in your theme's directory here: wp-admin-menu-classes.php and what follows shows how you might call the functions in your theme's
functions.php
file:<?php require_once('wp-admin-menu-classes.php'); add_action('admin_menu','my_admin_menu'); function my_admin_menu() { swap_admin_menu_sections('Pages','Posts'); // Swap location of Posts Section with Pages Section rename_admin_menu_section('Media','Photos & Video'); // Rename Media Section to "Photos & Video" delete_admin_menu_section('Links'); // Get rid of Links Section $movie_tags_item_array = get_admin_menu_item_array('Movies','Movie Tags'); // Save off the Movie Tags Menu update_admin_menu_section('Movies',array( // Rename two Movie Menu Items and Delete the Movie Tags Item array('rename-item','item'=>'Movies','new_title'=>'List Movies'), array('rename-item','item'=>'Add New','new_title'=>'Add Movie'), array('delete-item','item'=>'Movie Tags'), )); copy_admin_menu_item('Movies',array('Actors','Add New')); // Copy the 'Add New' over from Actors renamed_admin_menu_item('Movies','Add New','Add Actor'); // Rename copied Actor 'Add New' to 'Add Actor add_admin_menu_item('Movies',array( // (Another way to get a 'Add Actor' Link to a section.) 'title' => 'Alt Add Actor ', 'slug' => 'post-new.php?post_type=actor', ), array(// Add Back the Movie Tags at the end. 'where'=>'end' )); add_admin_menu_item('Movies',$movie_tags_item_array,array(// Add Back the Movie Tags at the end. 'where'=>'end' )); delete_admin_menu_section('Actors'); // Finally just get rid of the actors section }
What's more, these functions are even under consideration (as a base) for inclusion in WordPress 3.1 so if we're lucky these might even become standard!
-
*神奇*的API!我花了几个月的时间在现有框架中添加了我的第一个自定义菜单部分(这就是为什么我研究代码的原因很多)……但是您的API使用起来似乎更加直观!*Fantastic* addition to the API! It took me months to add my first custom menu section with the existing framework (which is why I studied the code do much) ... but your API seems much more intuitive to use!
- 0
- 2010-09-02
- EAMann
-
* @ EAMann *-谢谢!如果您想提出任何建议,甚至要合作进行改进,我希望能建立一些可以使每个人都更容易的东西,除了少数碰巧看到此问答的人.*@EAMann* - Thanks! If you want to offer any suggestions or even collaborate on improving it I'd love to get something established that could make it easier for everyone except for just the few who happen to see this Q&A.
- 0
- 2010-09-03
- MikeSchinkel
-
这是否成为核心,还是在管理管理员中菜单项的顺序方面仍需要这样做?did this make core or is this or something similar still required to manipulate the order of menu items in the admin?
- 0
- 2012-09-12
- Q Studio
-
据我所知,它已被推迟到WP 3.5As far as i have found, it's been postponed to WP 3.5
- 0
- 2012-09-22
- pixeline
-
嘿,迈克-wp_debug在此函数上引发错误:--------函数swap_admin_menu_sections($from_section,$to_section){ $from_section=get_admin_menu_section($from_section); 如果($from_section) $from_section-> swap_with($to_section); 返回$ section; } --- 注意:未定义的变量:部分Hey Mike - wp_debug throws an error on this function: -------- function swap_admin_menu_sections($from_section,$to_section) { $from_section = get_admin_menu_section($from_section); if ($from_section) $from_section->swap_with($to_section); return $section; } --- Notice: Undefined variable: section
- 0
- 2012-10-11
- Q Studio
-
- 2012-12-20
我意识到这是一个旧线程,但是我认为值得使用更简单的解决方案进行更新.请注意,此功能适用于3.5,尚未经过任何其他版本的测试.以下代码可以放在插件或functions.php文件中.
请参阅: http://codex.wordpress.org/Plugin_API/Filter_Reference/menu_order .进行了略微修改以适合原始海报的需求(不过,我希望他现在找到了解决方案...).
// Rearrange the admin menu function custom_menu_order($menu_ord) { if (!$menu_ord) return true; return array( 'index.php', // Dashboard 'edit.php?post_type=custom_type_one', // Custom type one 'edit.php?post_type=custom_type_two', // Custom type two 'edit.php?post_type=custom_type_three', // Custom type three 'edit.php?post_type=custom_type_four', // Custom type four 'edit.php?post_type=custom_type_five', // Custom type five 'separator1', // First separator 'edit.php?post_type=page', // Pages 'edit.php', // Posts 'upload.php', // Media 'link-manager.php', // Links 'edit-comments.php', // Comments 'separator2', // Second separator 'themes.php', // Appearance 'plugins.php', // Plugins 'users.php', // Users 'tools.php', // Tools 'options-general.php', // Settings 'separator-last', // Last separator ); } add_filter('custom_menu_order', 'custom_menu_order'); // Activate custom_menu_order add_filter('menu_order', 'custom_menu_order');
管理菜单中未在此处列出的所有项目都不会被删除.它们将被添加到菜单的底部.
I realize this is an old thread, but I think it's worth updating with a MUCH easier solution. Please note that this works with 3.5 and has not been tested with any other version. The following code can be placed in a plugin or the functions.php file.
See: http://codex.wordpress.org/Plugin_API/Filter_Reference/menu_order. Modified slightly to suit the original poster's needs (though, I hope he found a solution by now...).
// Rearrange the admin menu function custom_menu_order($menu_ord) { if (!$menu_ord) return true; return array( 'index.php', // Dashboard 'edit.php?post_type=custom_type_one', // Custom type one 'edit.php?post_type=custom_type_two', // Custom type two 'edit.php?post_type=custom_type_three', // Custom type three 'edit.php?post_type=custom_type_four', // Custom type four 'edit.php?post_type=custom_type_five', // Custom type five 'separator1', // First separator 'edit.php?post_type=page', // Pages 'edit.php', // Posts 'upload.php', // Media 'link-manager.php', // Links 'edit-comments.php', // Comments 'separator2', // Second separator 'themes.php', // Appearance 'plugins.php', // Plugins 'users.php', // Users 'tools.php', // Tools 'options-general.php', // Settings 'separator-last', // Last separator ); } add_filter('custom_menu_order', 'custom_menu_order'); // Activate custom_menu_order add_filter('menu_order', 'custom_menu_order');
Any items in the admin menu that are not listed here won't be removed. They will be appended to the bottom of the menu.
-
是的,这样更好,也可以重新排序您可以取消设置然后重新设置的子菜单项.Yes this is much better, also to re-order submenu items you can unset and then reset them.
- 1
- 2012-12-20
- Wyck
-
- 2010-09-04
我了解您不想使用插件,但为简单起见,请尝试使用Janis Elsts的Admin Menu Editor插件.您可以根据自己的喜好重新排列管理菜单;也可以隐藏菜单项.
I understand you don't want to use a plugin, but for pure simplicity, try the Admin Menu Editor plugin by Janis Elsts. Rearrange your admin menus any way you like; can also hide menu items.
-
我在多站点安装中尝试了此插件,效果最佳.但是,如果我们有多站点安装,则需要为每个子站点手动配置选项.I tried this plugin in multisite installation .It works best.But we need to manually configure the options for every sub-site if we have multisite Installation.
- 0
- 2010-10-13
- user391
-
- 2012-11-08
要移动菜单项,我喜欢使用全局
$menu
变量.例如,如果我想将"页面"菜单移到菜单底部,可以在
functions.php
或插件中使用它:function admin_menu_items() { global $menu; $menu[102]=$menu[20];//make menu 102 be the same as menu 20 (pages) $menu[20]=array();//make original pages menu disappear } add_action('admin_menu', 'admin_menu_items');
,如果我想交换"帖子和链接"菜单:
function admin_menu_items() { global $menu; $storemenu = $menu[15];//save links menu into $storemenu $menu[15] = $menu[5];//make links menu = posts menu $menu[5] = $storemenu; //make menu 5/posts = $storemenu/links } add_action('admin_menu', 'admin_menu_items');
使用此技巧已有一段时间,刚刚在WP 3.4.1中进行了测试
To move menu items around, I like using the Global
$menu
variable.For example, if I wanted to move the "Pages" menu to the bottom of the menus, I would use this in
functions.php
or a plugin:function admin_menu_items() { global $menu; $menu[102]=$menu[20];//make menu 102 be the same as menu 20 (pages) $menu[20]=array();//make original pages menu disappear } add_action('admin_menu', 'admin_menu_items');
and if I wanted to swap the Posts and Links menus:
function admin_menu_items() { global $menu; $storemenu = $menu[15];//save links menu into $storemenu $menu[15] = $menu[5];//make links menu = posts menu $menu[5] = $storemenu; //make menu 5/posts = $storemenu/links } add_action('admin_menu', 'admin_menu_items');
Been using this trick a little while, just tested with WP 3.4.1
-
我知道这是一篇较旧的文章,但是关于您的第一个摘要,我遇到了admin_menu_items函数中最后一行的问题,该问题已通过用unset($menu [20]);替换而得以解决.I know this is an older post but with regards to your first snippet I ran into an issue with the last line in the `admin_menu_items` function which was fixed by replacing it with `unset($menu[20]);`
- 0
- 2017-08-04
- hot_barbara
-
- 2012-03-05
太棒了.非常感谢. 我只是在我的functions.php中放了几行代码
require_once('/extras/wp-admin-menu-classes.php'); add_action('admin_menu','my_admin_menu'); function my_admin_menu() { swap_admin_menu_sections('Pages','Posts'); // Swop location of Posts Section with Pages Section }
另外将 wp-admin-menu-classes.php 放在我的主题文件夹中,现在将"帖子"按钮与"页面"按钮一同切换.
我希望这很快就会成为核心的一部分,这样我们就不需要在函数中编写整个菜单,只需重新排列两个按钮即可.
实际上,要为4个按钮获得更指定的顺序有点棘手. 为了将4个按钮中的更改为:页面,帖子,媒体,链接 我需要使用以下代码:
swap_admin_menu_sections('Pages','Posts'); swap_admin_menu_sections('Media','Links'); swap_admin_menu_sections('Posts','Links');
Awesome. Thank you so much. I just put some lines of code into my functions.php
require_once('/extras/wp-admin-menu-classes.php'); add_action('admin_menu','my_admin_menu'); function my_admin_menu() { swap_admin_menu_sections('Pages','Posts'); // Swop location of Posts Section with Pages Section }
Plus placing the wp-admin-menu-classes.php in my theme folder and now the 'posts' button is swopped with the 'pages' button.
I hope this will become part of the core soon and in a way so that we don't need to write the whole menu within a function to just reorder two buttons.
In fact it was a bit tricky to get a more specified order for 4 buttons. In order to change the of 4 buttons to: Pages, Post, Media, Links I needed to use the folowing code:
swap_admin_menu_sections('Pages','Posts'); swap_admin_menu_sections('Media','Links'); swap_admin_menu_sections('Posts','Links');
在花了几个小时试图完成这个相当简单的任务而没有运气之后,我对此感到有些沮丧.
基本上,我创建了5种自定义帖子类型,而我要做的就是直接在"仪表板" 下按特定顺序显示它们.
根据WordPress文档,您似乎无法真正做到这一点,因为最高菜单顺序似乎是" 5".以上 L
我猜有些专家在阅读本文时,可以通过使用功能文件而不使用插件(我知道存在)来按照我想要的方式订购管理菜单的简单方式.
请继续尝试创建5种单独的帖子类型,并按特定顺序将它们直接包含在仪表板下...似乎这是不可能的.?? ...是否有某种类型的jquery hack可以做到这一点某人可以与我共享的工作,或者最好是不使用jQuery的工作?