繁体   English   中英

Wordpress 将自定义分类法添加到自定义菜单

[英]Wordpress add custom taxonomy to custom menu

我搜索了又搜索,除了将自定义分类法添加到自定义管理菜单的“hack 方法”之外,找不到其他方法。

add_menu_page(
        'Practice Directory',
        'Practice Directory',
        'read',
        'practice-directory',
        '',
        'dashicons-welcome-widgets-menus',
        40
);

然后我注册我的帖子类型并确保它们使用

'show_in_menu'          => 'practice-directory',

这有效,自定义帖子类型显示在我的自定义菜单中。

但是自定义分类法不接受同一属性的字符串,只有 true 或 false。

    'show_in_menu'          => 'false',

所以要添加它你必须创建一个子菜单页面

add_submenu_page(
    'practice-directory',
    'Doctors',
    'Doctors',
    'edit_posts',
    'edit-tags.php?taxonomy=doctor',
    false
);

这是一种“黑客”的方式。

还有别的办法吗? 在不修改 WordPress 核心的情况下,我可以覆盖 register_taxonomy function 以便能够接受“show_in_menu”的字符串并遵循 register_post_type 的功能吗?

要求截图

在此处输入图像描述

我发现的唯一方法是像您一样创建子菜单,并在我们进入分类页面时将菜单设置为活动状态:

创建您的管理菜单:

add_menu_page('Page title', 'Menu title', 'read', 'menu_slug', false, 'dashicons-welcome-widgets-menus', 25);

如果需要,添加自定义帖子类型:

register_post_type('your_cpt_name', array(
    ...
    'show_in_menu' =>  'menu_slug',//your previously created menu slug
    ...
));

添加您的自定义分类:

register_taxonomy('your_taxonomy_name', 'your_cpt_name', $args);

添加您的分类子菜单:

add_submenu_page('menu_slug', 'Page Title', 'Menu Title', 'custom_post_capability', 'edit-tags.php?taxonomy=your_taxonomy_name&post_type=your_cpt_name', false);

激活时为菜单突出显示添加过滤器:

add_filter('parent_file', 'highlightTaxoSubMenu');

function highlightTaxoSubMenu($parent_file){
    global $submenu_file, $current_screen, $pagenow;
    if ($current_screen->post_type == 'your_cpt_name') {
        if ( $pagenow == 'edit-tags.php' && $current_screen->taxonomy == 'your_taxonomy_name' ) {
            $submenu_file = 'edit-tags.php?taxonomy=your_taxonomy_name&post_type=your_cpt_name';
        }
        $parent_file = 'menu_slug';//your parent menu slug
    }
    return $parent_file;
}

编辑:当您的 CPT 在管理中作为子菜单放置时,如果自定义用户角色没有“edit_posts”功能(即使他们具有您的 cpt 编辑功能),则无法为此 CPT 创建新帖子。 解决方法是在您的子菜单中为此自定义帖子添加“创建新帖子”链接

add_submenu_page('menu_slug', 'Add', 'Add', 'custom_post_capability', 'post-new.php?post_type=your_cpt_name', false);

转到外观->菜单,然后找到显示屏幕选项的顶部,然后单击该按钮,然后找到您需要在菜单中显示的内容

暂无
暂无

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

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