[英]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.