从自定义帖子类型中删除类别分类

时间:2019-08-21 作者:Freddy

我有一个名为“资源”的自定义帖子类型。

How I\'ve defined the taxonomy:

register_taxonomy(  
    \'resource\',  // the name of the taxonomy
    \'resources\', // post type name
    array(  
        \'hierarchical\' => true,  
        //\'label\' => \'Resources\',  // display name
        \'query_var\' => true,
        \'rewrite\' => array(
            \'slug\' => \'resources\', // This controls the base slug that will display before each term
            \'with_front\' => false // Don\'t display the category base before 
        )
    )  
);  

How I\'ve registered the post type:

register_post_type(
            \'resources\',
            tp_build_post_args(
                \'resources\', \'Resource\', \'Resources\',
                array(
                    \'menu_icon\'     => \'dashicons-welcome-write-blog\',
                    \'menu_position\' => 20,
                    \'has_archive\'   => true,
                    \'public\'      => true,
                    \'supports\' => array(\'editor\', \'title\',\'author\',\'thumbnail\', \'revisions\'),
                    \'taxonomies\' => array(\'post_tag\', \'type\', \'sector\')
                )
            )
        );
正如你所看到的,我没有category 在中选择的选项\'taxonomies\' => array(\'post_tag\', \'type\', \'sector\'), 但它在管理中显示为一个选项:

enter image description here

为什么会这样?

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

首先,我认为“类别”实际上是用于自定义分类法resource 而不是标准category 分类学但在菜单中,它被标记为Categories 因为您没有为resource 分类法(因为label 已注释掉,默认为Categories):

register_taxonomy(
    \'resource\',  // the name of the taxonomy
    \'resources\', // post type name
    array(
        \'hierarchical\' => true,
        //\'label\' => \'Resources\',  // display name <- this
        ...
    )
);
其次resource 分类法实际上分配给resources 帖子类型,尽管分类法不在帖子类型的列表中taxonomies 属性:

register_taxonomy(
    \'resource\',  // the name of the taxonomy
    \'resources\', // post type name <- here, the taxonomy will be assigned to this post type
    array( ... )
);
如果你真的不想resource 要分配给resources post type,然后将post type设置为null 或者不管合适的职位类型是什么:

register_taxonomy(
    \'resource\',  // the name of the taxonomy
    null,        // set to null, which means don\'t assign to `resources` or any post types..
    array( ... )
);
或者,您可以使用unregister_taxonomy_for_object_type() 要从帖子类型取消分配分类,请执行以下操作:

// Call after the post type and taxonomy are both registered.
unregister_taxonomy_for_object_type( \'resource\', \'resources\' );

相关推荐

taxonomy terms, inverted

我有一个分类法,它将是品牌,儿童项目是模板。我有密码single.php 显示详细信息。 <div class=\"box2\"> <div class=\"faixa\"> <div class=\"marca\">MARCA <div class=\"marca\"> <?php $term_