【发布时间】:2010-11-22 20:04:16
【问题描述】:
我正在创建不同的自定义帖子类型和分类法,我想从默认的“帖子”帖子类型中删除“帖子标签”分类法。我该怎么做?
谢谢。
【问题讨论】:
标签: wordpress wordpress-theming taxonomy
我正在创建不同的自定义帖子类型和分类法,我想从默认的“帖子”帖子类型中删除“帖子标签”分类法。我该怎么做?
谢谢。
【问题讨论】:
标签: wordpress wordpress-theming taxonomy
我建议你不要乱用实际的全局。简单地从帖子类型中取消注册分类法更安全: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' );
【讨论】:
function remove_menus(){ remove_menu_page('edit-tags.php?taxonomy=post_tag'); // Post tags } add_action( 'admin_menu', 'remove_menus' );
register_taxonomy( 'post_tag', array(), array('show_in_nav_menus' => false) );
也许技术上更正确的方法是使用unregister_taxonomy_for_object_type
add_action( 'init', 'unregister_tags' );
function unregister_tags() {
unregister_taxonomy_for_object_type( 'post_tag', 'post' );
}
【讨论】:
if ( is_object_in_taxonomy( 'post', 'post_tag' ) ) { ... } 删除之前检查分类法是否已与对象类型相关联
“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');
}
【讨论】: