ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-07-25 08:40:43  阅读:227  来源: 互联网

标签:php wordpress wordpress-theming wordpress-plugin


我注册了自定义帖子类型,我不希望它有自己的菜单,而是我想把它作为现有管理菜单项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页面……

有帮助吗?

解决方法:

将Custom-Post-Type放置在现有父页面的子菜单中

根据食典委的说法,这是一种已知的预期行为:

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.

资料来源:https://codex.wordpress.org/Function_Reference/register_post_type#Arguments(参见“show_in_menu”部分)

这是报价的结尾,它提供了一个解决方案:

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.

所以这很容易解决.但是在我的情况下,我无法更改父页面的优先级,因为它是由第三方库生成的.因此我提出了这个解决方案:

// 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 I时将“show_in_menu”参数设置为false,因此我们可以通过add_submenu_page手动将其添加到菜单中,如上所示.

正确将Custom-Post-Type子菜单项设置为“活动”

现在,上面的解决方案工作正常,但是在创建/编辑“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;
}

微调:重命名第一个子菜单条目

此外,我还希望我的子菜单的第一个菜单条目的名称与父名称不同.默认情况下,使用上面的代码,这就是我们所拥有的:

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

如您所见,子菜单的第一个菜单项是父菜单的副本,这是默认的WordPress行为.我想将这个重复的条目重命名为不同的东西,就像WordPress使用默认菜单一样(例如“帖子”和子菜单条目“所有帖子”,它们都指向同一页面,但命名方式不同).

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

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,因此在创建后重命名.现在我们有:

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

请注意,“子菜单文本”指向与“示例父页面”相同的位置.

标签:php,wordpress,wordpress-theming,wordpress-plugin
来源: https://codeday.me/bug/20190725/1531492.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有