注册新的自定义帖子类型时使用自定义分类

时间:2015-01-12 作者:user1048676

我正在创建自己的自定义帖子类型,下面是代码:

add_action( \'init\', \'create_gallery_post_type\' );
function create_gallery_post_type() {
  register_post_type( \'gallery\',
    array(
      \'labels\' => array(
        \'name\' => __( \'Galleries\' ),
        \'singular_name\' => __( \'Gallery\' )
      ),
        \'publicly_queryable\' => true,
        \'show_ui\' => true,
        \'query_var\' => true,
        \'rewrite\' => true,
        \'capability_type\' => \'post\',
        \'hierarchical\' => false,
        \'menu_position\' => null,
        \'taxonomies\' => array(\'category\'),
        \'supports\' => array(\'title\',\'editor\',\'thumbnail\')
    )
  );
}
我有另一个自定义帖子类型,它有自己的自定义类别分类法,叫做“公文包类别”。我想对这个帖子类型使用相同的分类法,因为它们是相同的值,我只需要在一个地方输入它们。

我试着让它说:

\'taxonomies\' => array(\'portfolio-category\')
但这并没有显示我的图库帖子类型下的类别链接。有什么想法吗?谢谢

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

你所拥有的原则上应该是可行的。请注意taxonomies 参数需要现有的分类法,即在注册CPT时已经注册的分类法。

如果要在注册过程中正确把握时间有问题,您还可以使用register_taxonomy_for_object_type() 从注册过程中分别创建两者之间的关联。

结束

相关推荐