如何从 Wordpress 中删除分类法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4249694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:41:02  来源:igfitidea点击:

How do I remove a taxonomy from Wordpress?

wordpresswordpress-themingtaxonomy

提问by Evan

I'm creating different custom post types and taxonomies and I want to remove the 'Post Tags' taxonomy from the default 'Posts' post type. How do I go about doing this?

我正在创建不同的自定义帖子类型和分类法,我想从默认的“帖子”帖子类型中删除“帖子标签”分类法。我该怎么做?

Thanks.

谢谢。

回答by Evan

I suggest you don't mess with the actual global. Its safer to simply deregister the taxonomy from the post type: register_taxonomy is used for both creation and modification.

我建议你不要搞乱实际的全局。简单地从帖子类型中取消注册分类法更安全: register_taxonomy 用于创建和修改。

function ev_unregister_taxonomy(){
    register_taxonomy('post_tag', array());
}
add_action('init', 'ev_unregister_taxonomy');

To remove the sidebar menu entry:

要删除侧边栏菜单条目:

// Remove menu
function remove_menus(){
    remove_menu_page('edit-tags.php?taxonomy=post_tag'); // Post tags
}

add_action( 'admin_menu', 'remove_menus' );

回答by cameronjonesweb

Perhaps a more technically correct method would be to use unregister_taxonomy_for_object_type

也许技术上更正确的方法是使用 unregister_taxonomy_for_object_type

add_action( 'init', 'unregister_tags' );

function unregister_tags() {
    unregister_taxonomy_for_object_type( 'post_tag', 'post' );
}

回答by cnotethegr8

Where it says 'taxonomy_to_remove' is where you'll enter the taxonomy you want to remove. For instance you can replace it with the existing, post_tagor category.

' taxonomy_to_remove' 是您输入要删除的分类法的位置。例如,您可以将其替换为现有的post_tagcategory.

add_action( 'init', 'unregister_taxonomy');
function unregister_taxonomy(){
    global $wp_taxonomies;
    $taxonomy = 'taxonomy_to_remove';
    if ( taxonomy_exists( $taxonomy))
        unset( $wp_taxonomies[$taxonomy]);
}

回答by Den Media

Total unregister and remove (minimal PHP version 5.4!)

完全注销和删除(最低 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');
    });

回答by Mahfuzul Hasan

There is new function to remove taxonomy from WordPress.

有一个新功能可以从 WordPress 中删除分类法。

Use unregister_taxonomy( string $taxonomy ) function

Use unregister_taxonomy( string $taxonomy ) function

See details: https://developer.wordpress.org/reference/functions/unregister_taxonomy/

查看详情:https: //developer.wordpress.org/reference/functions/unregister_taxonomy/

回答by user2957058

Use it in 'admin_init' hook insetead not 'init'

在“admin_init”钩子插入而不是“init”中使用它

function unregister_taxonomy(){
    register_taxonomy('post_tag', array());
}
add_action('admin_init', 'unregister_taxonomy');

回答by Vlad

add_action('admin_menu', 'remove_menu_items'); function remove_menu_items() { remove_submenu_page('edit.php','edit-tags.php?taxonomy=post_tag'); }

add_action('admin_menu', 'remove_menu_items'); function remove_menu_items() { remove_submenu_page('edit.php','edit-tags.php?taxonomy=post_tag'); }