本文介绍了WordPress 管理员:将自定义帖子类型作为父菜单的子菜单时,CPT 会覆盖父菜单链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注册了一个自定义帖子类型,但我不希望它有自己的菜单,而是希望将它作为名为 my-custom-parent-page.

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.

这是我的代码:

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',
    )
);

它有效,这意味着它正确地位于菜单 my-custom-parent-page 下,但是现在当我单击父菜单(即 my-custom-parent-page) 它将我指向 my_custom_post_type 页面...

It works, meaning that it's properly located under the menu my-custom-parent-page, however now when I click on the parent menu (i.e. my-custom-parent-page) it points me to the my_custom_post_type page...

有什么帮助吗?

推荐答案

在现有父页面的子菜单中放置自定义帖子类型

根据 Codex,这是一种已知且预期的行为:

Place a Custom-Post-Type in an submenu of an existing parent page

According to the Codex, this is a known and expected behavior:

注意:当使用'some string'显示为插件创建的菜单页面的子菜单时,此项将成为第一个子菜单项,并替换顶级链接的位置.

来源:https://codex.wordpress.org/Function_Reference/register_post_type#Arguments(见show_in_menu"部分)

Source: https://codex.wordpress.org/Function_Reference/register_post_type#Arguments (See the "show_in_menu" section)

这是提供解决方案的报价的结尾:

Here is the end of the quote which offers a solution:

如果不需要,创建菜单页面的插件需要将 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');
}

请注意优先级为 11,并且在注册 Custom-Post-Type 时我将show_in_menu"参数设置为 false,因此我们可以将其添加到通过 add_submenu_page 手动菜单,如上所示.

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.

现在,上述解决方案工作正常,但是在创建/编辑example_cpt"自定义帖子类型的帖子时,它未设置为活动状态且子菜单未展开.以下是如何确保在创建/编辑example_cpt"自定义帖子类型的帖子时将其设置为活动状态,以及它所在的子菜单正确设置为活动状态:

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. 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:

// 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

如您所见,子菜单的第一个菜单条目是父菜单的副本,这是 WordPress 的默认行为.我想将此重复条目重命名为不同的名称,就像 WordPress 对默认菜单所做的一样(例如帖子"和子菜单条目所有帖子",它们都指向同一页面但名称不同).

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. 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).

以下是重命名第一个子菜单条目的方法:

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');

}

请注意优先级为 11,因此它在创建后 被重命名.现在我们有:

Please note the priority 11, so it is renamed after it has been created. 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".

这篇关于WordPress 管理员:将自定义帖子类型作为父菜单的子菜单时,CPT 会覆盖父菜单链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 02:48