如何从 WordPress 中删除分类法?
我正在创建不同的自定义文章类型和分类法,我想从默认的“文章”文章类型中删除“文章标签”分类法。 我该怎么做?
解决方案
我建议你不这样做。 简单地从文章类型中取消注册分类法更安全: register_taxonomy 用于创建和修改。
function ev_unregister_taxonomy(){
register_taxonomy('post_tag', array());
}
add_action('init', 'ev_unregister_taxonomy');
要删除侧边栏菜单条目:
// Remove menu
function remove_menus(){
remove_menu_page('edit-tags.php?taxonomy=post_tag'); // Post tags
}
add_action( 'admin_menu', 'remove_menus' );
也许技术上更正确的方法是使用unregister_taxonomy_for_object_type
add_action( 'init', 'unregister_tags' );
function unregister_tags() {
unregister_taxonomy_for_object_type( 'post_tag', 'post' );
}
它说“ taxonomy_to_remove
”的地方是您输入要删除的分类taxonomy_to_remove
的地方。 例如,您可以将其替换为现有的post_tag
或category
。
add_action( 'init', 'unregister_taxonomy');
function unregister_taxonomy(){
global $wp_taxonomies;
$taxonomy = 'taxonomy_to_remove';
if (taxonomy_exists( $taxonomy))
unset( $wp_taxonomies[$taxonomy]);
}
完全注销和删除(最低 PHP 版本 5.4!)
add_action('init', function(){
global $wp_taxonomies;
unregister_taxonomy_for_object_type( 'category', 'post' );
unregister_taxonomy_for_object_type( 'post_tag', 'post' );
if ( taxonomy_exists( 'category'))
unset( $wp_taxonomies['category']);
if ( taxonomy_exists( 'post_tag'))
unset( $wp_taxonomies['post_tag']);
unregister_taxonomy('category');
unregister_taxonomy('post_tag');
});
有一个新功能可以从 WordPress 中删除分类法。
Use unregister_taxonomy( string $taxonomy ) function
查看详情: https://developer.wordpress.org/reference/functions/unregister_taxonomy/
在“admin_init”钩子插入而不是“init”中使用它
function unregister_taxonomy(){
register_taxonomy('post_tag', array());
}
add_action('admin_init', 'unregister_taxonomy');
add_action('admin_menu', 'remove_menu_items');
function remove_menu_items() {
remove_submenu_page('edit.php','edit-tags.php?taxonomy=post_tag');
}