如何修改已注册的分类

时间:2014-09-17 作者:mrwweb

今天,我需要更改已由third party plugin. 我特别想设置show_admin_column 参数到true 并更改rewrite 所以它不仅仅是分类上的鼻涕虫。在本例中,它是一个带有“人员类别”自定义分类法的“人员”帖子类型。

我很惊讶这之前没有被问到,所以这里有一个问题和答案。

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

register_taxonomy() 是工作的工具。抄本:

此函数用于添加或覆盖分类法。

一种选择是复制register_taxonomy() $args 并对其进行修改。然而,这意味着未来对原始register_taxonomy() 代码将被覆盖。

因此,至少在这种情况下,最好获取原始参数,修改我想要更改的参数,然后重新注册分类法。此解决方案的灵感来自于本answer to a similar question about custom post types.

使用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() 可以处理objectarray. 也就是说,register_taxonomy()\'s$args 应作为array 根据法典,我觉得这是对的。

结束

相关推荐