在Wordpress中,如何向主题创建的帖子类型添加分类法?

在Wordpress中,如何向主题创建的帖子类型添加分类法?,wordpress,Wordpress,我使用的主题包括一个自定义的帖子类型。我需要添加类别和标签,这个职位类型。为了避免编辑主题或创建子主题,我创建了一个简单的插件,我认为可以添加分类法: add_action( 'plugins_loaded', 'gp_register_taxonomy_for_object_type' ); function gp_register_taxonomy_for_object_type() { register_taxonomy_for_object_type( 'post_tag', '

我使用的主题包括一个自定义的帖子类型。我需要添加类别和标签,这个职位类型。为了避免编辑主题或创建子主题,我创建了一个简单的插件,我认为可以添加分类法:

add_action( 'plugins_loaded', 'gp_register_taxonomy_for_object_type' );
function gp_register_taxonomy_for_object_type() {
    register_taxonomy_for_object_type( 'post_tag', 'service' );
    register_taxonomy_for_object_type( 'category', 'service' );
};

我尝试了
init
而不是
plugins\u loaded
,但也没有成功。

结果是
admin\u init
是这里的金票:

add_action( 'admin_init', 'gp_register_taxonomy_for_object_type' );
function gp_register_taxonomy_for_object_type() {
    register_taxonomy_for_object_type( 'post_tag', 'service' );
    register_taxonomy_for_object_type( 'category', 'service' );
};

您将看到加载的
plugins\u
操作在加载主题之前被触发。主题可以挂接的第一个操作是设置主题后的
操作


查看该文件。

将其添加到function.php中

add_action( 'admin_init', 'register_taxonomy_for_new_post_type' );
function register_taxonomy_for_new_post_type() {
    register_taxonomy_for_new_post_type( 'post_tag', 'post_type_name' );
    register_taxonomy_for_new_post_type( 'category', 'post_type_name' );
}