如何修改已注册的分类法


18

今天,我需要更改已由第三方插件注册的自定义分类法的参数。具体来说,我想将show_admin_column参数设置为true并更改该段,rewrite以便它不仅仅是分类标准段。在这种情况下,它是具有“人类别”自定义分类法的“人”帖子类型。

我很惊讶以前没有问过这个,所以这是一个问答。


对于任何陷入困境的人来说,它只是一个旁注,请记住在检查结果之前冲洗永久链接。
un.pez.vivo

Answers:


22

register_taxonomy()是工作的工具。从食典:

此功能添加或覆盖分类法。

一种选择是复制register_taxonomy() $args和修改它们。但是,这意味着将来对原始register_taxonomy()代码的任何更改都将被覆盖。

因此,至少在这种情况下,最好获取原始参数,修改我想要更改的参数,然后重新注册分类法。此解决方案的灵感来自@Otto,它回答了有关自定义帖子类型的类似问题

使用示例中的people自定义帖子类型和people_category分类法,可以做到这一点:

function wpse_modify_taxonomy() {
    // get the arguments of the already-registered taxonomy
    $people_category_args = get_taxonomy( 'people_category' ); // returns an object

    // make changes to the args
    // in this example there are three changes
    // again, note that it's an object
    $people_category_args->show_admin_column = true;
    $people_category_args->rewrite['slug'] = 'people';
    $people_category_args->rewrite['with_front'] = false;

    // re-register the taxonomy
    register_taxonomy( 'people_category', 'people', (array) $people_category_args );
}
// hook it up to 11 so that it overrides the original register_taxonomy function
add_action( 'init', 'wpse_modify_taxonomy', 11 );

注意,上面我将第三个register_taxonomy()参数转换为期望的数组类型。由于可以处理或的register_taxonomy()用途wp_parse_args(),因此这并非绝对必要。这就是说,的都应该是作为一份根据食品法典委员会,所以这种感觉对我。objectarrayregister_taxonomy()$argsarray


我正在尝试执行此操作,以将统一分类法更改为分层分类法。我只是将阶层属性更改为true。这样,它会在帖子编辑屏幕的右侧导致两个用于分类法的元框,而我实际上无法添加分类法...
Joel Worsham 2015年

确保使用与'people_category'原始分类法相同的标记(例如),以便将其覆盖。
mrwweb

1
天才!天才!完美地工作!
DaveyJake

1
我发现此解决方案存在问题:capabilities参数在内部存储,cap因此不会传递给新的注册分类法。在这里查看我对类似问题的回答
Fabien Quatravaux

1
太好了,我用它向自定义分类法中添加了层次化网址。
Brian Peat
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.