简体   繁体   English

WordPress管理员:将自定义帖子类型作为父菜单的子菜单放置时,CPT会覆盖父菜单链接

[英]WordPress Admin: When placing a Custom Post Type as a submenu of a parent menu, the parent menu link is being overridden by the CPT

I register a Custom Post Type, and I don't want it to have its own menu, instead I want to place it as a submenu of an existing admin menu item called my-custom-parent-page . 我注册了自定义帖子类型,我不希望它有自己的菜单,而是我想把它作为现有管理菜单项my-custom-parent-page的子菜单。

Here's my code: 这是我的代码:

register_post_type('my_custom_post_type',
    array(
        'labels' => array(              
            'name'               => __('Books', 'mcpt'),
            'singular_name'      => __('Book', 'mcpt'),
        ),
        'supports' => array('title', 'editor'),
        'show_ui' => true,
        'show_in_nav_menus' => false,
        'show_in_menu' => 'my-custom-parent-page',
    )
);

It works, meaning that it's properly located under the menu my-custom-parent-page , however now when I click on the parent menu (ie my-custom-parent-page ) it points me to the my_custom_post_type page... 它工作正常,这意味着它正确地位于菜单my-custom-parent-page ,但是现在当我点击父菜单(即my-custom-parent-page )时,它指向my_custom_post_type页面......

Any help? 有帮助吗?

Place a Custom-Post-Type in an submenu of an existing parent page 将Custom-Post-Type放置在现有父页面的子菜单中

According to the Codex, this is a known and expected behavior: 根据食典委的说法,这是一种已知的预期行为:

Note: When using 'some string' to show as a submenu of a menu page created by a plugin, this item will become the first submenu item, and replace the location of the top level link . 注意:当使用“some string”作为插件创建的菜单页面的子菜单时, 此项目将成为第一个子菜单项,并替换顶级链接的位置

Source: https://codex.wordpress.org/Function_Reference/register_post_type#Arguments (See the "show_in_menu" section) 来源: https ://codex.wordpress.org/Function_Reference/register_post_type#Arguments(参见“show_in_menu”部分)

Here is the end of the quote which offers a solution: 这是报价的结尾,它提供了一个解决方案:

If this isn't desired, the plugin that creates the menu page needs to set the add_action priority for admin_menu to 9 or lower. 如果不需要,创建菜单页面的插件需要将admin_menu的add_action优先级设置为9或更低。

So this is quite simple to solve. 所以这很容易解决。 However in my case I couldn't change the priority of the parent page because it is generated by a third-party library. 但是在我的情况下,我无法更改父页面的优先级,因为它是由第三方库生成的。 Therefore I came up with this solution: 因此我提出了这个解决方案:

// Move the "example_cpt" Custom-Post-Type to be a submenu of the "example_parent_page_id" admin page.
add_action('admin_menu', 'fix_admin_menu_submenu', 11);
function fix_admin_menu_submenu() {

    // Add "Example CPT" Custom-Post-Type as submenu of the "Example Parent Page" page
    add_submenu_page('example_parent_page_id', 'Example CPT', 'Example CPT', 'edit_pages' , 'edit.php?post_type=example_cpt');
}

Please note the priority 11, and also when registering the Custom-Post-Type I set the " show_in_menu " parameter to false , so we can add it in the menu manually via add_submenu_page as shown above. 请注意优先级11,并且在注册Custom-Post-Type I时将“ show_in_menu ”参数设置为false ,因此我们可以通过add_submenu_page手动将其添加到菜单中,如上所示。


Properly set the Custom-Post-Type submenu entry as "active" 正确将Custom-Post-Type子菜单项设置为“活动”

Now, the above solution works fine, however when creating/editing a post of the "example_cpt" Custom-Post-Type, it is not set as active and the submenu is not unfolded. 现在,上面的解决方案工作正常,但是在创建/编辑“example_cpt”Custom-Post-Type的帖子时,它没有设置为活动状态且子菜单未展开。 Here is how to make sure that it is set as active, as well as the submenu in which it resides is properly set as active when creating/editing a post of the "example_cpt" Custom-Post-Type: 以下是如何确保将其设置为活动状态,以及在创建/编辑“example_cpt”Custom-Post-Type的帖子时将其所在的子菜单正确设置为活动状态:

// Set the "example_parent_page_id" submenu as active/current when creating/editing a "example_cpt" post
add_filter('parent_file', 'fix_admin_parent_file');
function fix_admin_parent_file($parent_file){
    global $submenu_file, $current_screen;

    // Set correct active/current menu and submenu in the WordPress Admin menu for the "example_cpt" Add-New/Edit/List
    if($current_screen->post_type == 'example_cpt') {
        $submenu_file = 'edit.php?post_type=example_cpt';
        $parent_file = 'example_parent_page_id';
    }
    return $parent_file;
}

Fine-tuning: Rename the first submenu entry 微调:重命名第一个子菜单条目

Furthermore, I also wanted the first menu entry of my submenu to be named differently from the parent name. 此外,我还希望我的子菜单的第一个菜单条目的名称与父名称不同。 By default, and using the code above, this is what we have: 默认情况下,使用上面的代码,这就是我们所拥有的:

- Example Parent Page
-- Example Parent Page
-- Example CPT

So as you can see, the first menu entry of the submenu is a duplicate of the parent menu, and this is the default WordPress behavior. 如您所见,子菜单的第一个菜单项是父菜单的副本,这是默认的WordPress行为。 I wanted to rename this duplicate entry to something different, much like WordPress does with the default menus (for example "Posts" and the submenu entry "All Posts" which both point to the same page but are named differently). 我想将这个重复的条目重命名为不同的东西,就像WordPress使用默认菜单一样(例如“帖子”和子菜单条目“所有帖子”,它们都指向同一页面,但命名方式不同)。

Here is how to rename the first submenu entry: 以下是重命名第一个子菜单条目的方法:

add_action('admin_menu', 'rename_first_submenu_entry', 11);
function rename_first_submenu_entry() {

    // Rename first submenu entry (duplicate of parent menu) from "Example Parent Page" to "Submenu Text"
    add_submenu_page('example_parent_page_id', 'Example Parent Page', 'Submenu Text', 'edit_pages' , 'example_parent_page_id');

}

Please note the priority 11, so it is renamed after it has been created. 请注意优先级11,因此在创建重命名。 And now we have: 现在我们有:

- Example Parent Page
-- Submenu Text
-- Example CPT

Please note that "Submenu Text" points to the same location as "Example Parent Page". 请注意,“子菜单文本”指向与“示例父页面”相同的位置。

You also can simply set 'show_in_menu' in custom post type args to $menu_slug that you set in add_menu_page() that you want to set the CPT as sub menu of and set the priority of admin_menu function to 9 or lower. 您还可以将自定义帖子类型args中的'show_in_menu'设置为您在add_menu_page()中设置的$menu_slug ,您要将CPT设置为子菜单,并将admin_menu函数的优先级设置为9或更低。 For example: 例如:

First, create a new top-level menu page, with priority set to 9 or lower (it's a must): 首先,创建一个新的顶级菜单页面,优先级设置为9或更低(这是必须的):

add_action( 'admin_menu', 'settings_menu' ), 9 );

function settings_menu() {

    add_menu_page( __( 'Page Title' ), 'Menu Title', 'manage_options', 'menu_slug', show_page_callback() );
}

function show_page_callback() {

    // show the settings page, plugin homepage, etc.
}

Then create custom post type with 'show_in_menu' arg set to menu_slug that we just set in settings_menu() function. 然后创建自定义帖子类型,将'show_in_menu'arg设置为我们刚刚在settings_menu()函数中settings_menu() menu_slug

add_action( 'init', 'create_post_type' );

function create_post_type() {

register_post_type('my_custom_post_type',
    array(
        'labels' => array(              
            'name'               => __('Books', 'mcpt'),
            'singular_name'      => __('Book', 'mcpt'),
        ),
        'supports' => array('title', 'editor'),
        'public' => true,
        'show_in_menu' => 'menu_slug',
    );
}

Hope it helps. 希望能帮助到你。

can't say, what's exactly the reason, but it seems wordpress redirects to the first submenu-item. 不能说,究竟是什么原因,但似乎wordpress重定向到第一个子菜单项。

So you have to create a new sub-menu-item with the same contents of your parent-menu-item. 因此,您必须创建一个新的子菜单项,其中包含与父菜单项相同的内容。

add_action('admin_menu', 'my_admin_menu');

function my_admin_menu() {
    global $submenu;
    add_menu_page('My Menu', 'My Menu', 'administrator', 'my-menu', 'callback_func');

    $parent = array('My Menu', 'administrator', 'my-menu', 'My Menu'); // new submenu-itm
    $submenu['my-menu'] = fix_menu($submenu['my-menu'], $parent); // adds the new submenu-item at beginning of 'my-menu'-item
}

function fix_menu($submenu) {
    array_unshift ($submenu, $parent);
    return $submenu;
}

Hope it works for you. 希望对你有效。

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

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